
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.db.models.query &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-db-models-query">
            
  <h1>Source code for django.db.models.query</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">The main QuerySet implementation. This provides the public API for the ORM.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">operator</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">OrderedDict</span><span class="p">,</span> <span class="n">namedtuple</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">lru_cache</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">chain</span>

<span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.core</span> <span class="k">import</span> <span class="n">exceptions</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">DJANGO_VERSION_PICKLE_KEY</span><span class="p">,</span> <span class="n">IntegrityError</span><span class="p">,</span> <span class="n">connections</span><span class="p">,</span> <span class="n">router</span><span class="p">,</span>
    <span class="n">transaction</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.db.models</span> <span class="k">import</span> <span class="n">DateField</span><span class="p">,</span> <span class="n">DateTimeField</span><span class="p">,</span> <span class="n">sql</span>
<span class="kn">from</span> <span class="nn">django.db.models.constants</span> <span class="k">import</span> <span class="n">LOOKUP_SEP</span>
<span class="kn">from</span> <span class="nn">django.db.models.deletion</span> <span class="k">import</span> <span class="n">Collector</span>
<span class="kn">from</span> <span class="nn">django.db.models.expressions</span> <span class="k">import</span> <span class="n">Case</span><span class="p">,</span> <span class="n">Expression</span><span class="p">,</span> <span class="n">F</span><span class="p">,</span> <span class="n">Value</span><span class="p">,</span> <span class="n">When</span>
<span class="kn">from</span> <span class="nn">django.db.models.fields</span> <span class="k">import</span> <span class="n">AutoField</span>
<span class="kn">from</span> <span class="nn">django.db.models.functions</span> <span class="k">import</span> <span class="n">Cast</span><span class="p">,</span> <span class="n">Trunc</span>
<span class="kn">from</span> <span class="nn">django.db.models.query_utils</span> <span class="k">import</span> <span class="n">FilteredRelation</span><span class="p">,</span> <span class="n">InvalidQuery</span><span class="p">,</span> <span class="n">Q</span>
<span class="kn">from</span> <span class="nn">django.db.models.sql.constants</span> <span class="k">import</span> <span class="n">CURSOR</span><span class="p">,</span> <span class="n">GET_ITERATOR_CHUNK_SIZE</span>
<span class="kn">from</span> <span class="nn">django.db.utils</span> <span class="k">import</span> <span class="n">NotSupportedError</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="k">import</span> <span class="n">timezone</span>
<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="k">import</span> <span class="n">RemovedInDjango30Warning</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="k">import</span> <span class="n">cached_property</span><span class="p">,</span> <span class="n">partition</span>
<span class="kn">from</span> <span class="nn">django.utils.version</span> <span class="k">import</span> <span class="n">get_version</span>

<span class="c1"># The maximum number of items to display in a QuerySet.__repr__</span>
<span class="n">REPR_OUTPUT_SIZE</span> <span class="o">=</span> <span class="mi">20</span>

<span class="c1"># Pull into this namespace for backwards compatibility.</span>
<span class="n">EmptyResultSet</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">EmptyResultSet</span>


<span class="k">class</span> <span class="nc">BaseIterable</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">chunked_fetch</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">chunk_size</span><span class="o">=</span><span class="n">GET_ITERATOR_CHUNK_SIZE</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">chunked_fetch</span> <span class="o">=</span> <span class="n">chunked_fetch</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">chunk_size</span> <span class="o">=</span> <span class="n">chunk_size</span>


<span class="k">class</span> <span class="nc">ModelIterable</span><span class="p">(</span><span class="n">BaseIterable</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Iterable that yields a model instance for each row.&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span>
        <span class="n">db</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">db</span>
        <span class="n">compiler</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">get_compiler</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">db</span><span class="p">)</span>
        <span class="c1"># Execute the query. This will also fill compiler.select, klass_info,</span>
        <span class="c1"># and annotations.</span>
        <span class="n">results</span> <span class="o">=</span> <span class="n">compiler</span><span class="o">.</span><span class="n">execute_sql</span><span class="p">(</span><span class="n">chunked_fetch</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">chunked_fetch</span><span class="p">,</span> <span class="n">chunk_size</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">chunk_size</span><span class="p">)</span>
        <span class="n">select</span><span class="p">,</span> <span class="n">klass_info</span><span class="p">,</span> <span class="n">annotation_col_map</span> <span class="o">=</span> <span class="p">(</span><span class="n">compiler</span><span class="o">.</span><span class="n">select</span><span class="p">,</span> <span class="n">compiler</span><span class="o">.</span><span class="n">klass_info</span><span class="p">,</span>
                                                  <span class="n">compiler</span><span class="o">.</span><span class="n">annotation_col_map</span><span class="p">)</span>
        <span class="n">model_cls</span> <span class="o">=</span> <span class="n">klass_info</span><span class="p">[</span><span class="s1">&#39;model&#39;</span><span class="p">]</span>
        <span class="n">select_fields</span> <span class="o">=</span> <span class="n">klass_info</span><span class="p">[</span><span class="s1">&#39;select_fields&#39;</span><span class="p">]</span>
        <span class="n">model_fields_start</span><span class="p">,</span> <span class="n">model_fields_end</span> <span class="o">=</span> <span class="n">select_fields</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">select_fields</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="n">init_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">attname</span>
                     <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">select</span><span class="p">[</span><span class="n">model_fields_start</span><span class="p">:</span><span class="n">model_fields_end</span><span class="p">]]</span>
        <span class="n">related_populators</span> <span class="o">=</span> <span class="n">get_related_populators</span><span class="p">(</span><span class="n">klass_info</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="n">db</span><span class="p">)</span>
        <span class="n">known_related_objects</span> <span class="o">=</span> <span class="p">[</span>
            <span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">related_objs</span><span class="p">,</span> <span class="n">operator</span><span class="o">.</span><span class="n">attrgetter</span><span class="p">(</span><span class="o">*</span><span class="p">[</span>
                <span class="n">field</span><span class="o">.</span><span class="n">attname</span>
                <span class="k">if</span> <span class="n">from_field</span> <span class="o">==</span> <span class="s1">&#39;self&#39;</span> <span class="k">else</span>
                <span class="n">queryset</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">from_field</span><span class="p">)</span><span class="o">.</span><span class="n">attname</span>
                <span class="k">for</span> <span class="n">from_field</span> <span class="ow">in</span> <span class="n">field</span><span class="o">.</span><span class="n">from_fields</span>
            <span class="p">]))</span> <span class="k">for</span> <span class="n">field</span><span class="p">,</span> <span class="n">related_objs</span> <span class="ow">in</span> <span class="n">queryset</span><span class="o">.</span><span class="n">_known_related_objects</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
        <span class="p">]</span>
        <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">compiler</span><span class="o">.</span><span class="n">results_iter</span><span class="p">(</span><span class="n">results</span><span class="p">):</span>
            <span class="n">obj</span> <span class="o">=</span> <span class="n">model_cls</span><span class="o">.</span><span class="n">from_db</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">init_list</span><span class="p">,</span> <span class="n">row</span><span class="p">[</span><span class="n">model_fields_start</span><span class="p">:</span><span class="n">model_fields_end</span><span class="p">])</span>
            <span class="k">for</span> <span class="n">rel_populator</span> <span class="ow">in</span> <span class="n">related_populators</span><span class="p">:</span>
                <span class="n">rel_populator</span><span class="o">.</span><span class="n">populate</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">annotation_col_map</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">attr_name</span><span class="p">,</span> <span class="n">col_pos</span> <span class="ow">in</span> <span class="n">annotation_col_map</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">attr_name</span><span class="p">,</span> <span class="n">row</span><span class="p">[</span><span class="n">col_pos</span><span class="p">])</span>

            <span class="c1"># Add the known related objects to the model.</span>
            <span class="k">for</span> <span class="n">field</span><span class="p">,</span> <span class="n">rel_objs</span><span class="p">,</span> <span class="n">rel_getter</span> <span class="ow">in</span> <span class="n">known_related_objects</span><span class="p">:</span>
                <span class="c1"># Avoid overwriting objects loaded by, e.g., select_related().</span>
                <span class="k">if</span> <span class="n">field</span><span class="o">.</span><span class="n">is_cached</span><span class="p">(</span><span class="n">obj</span><span class="p">):</span>
                    <span class="k">continue</span>
                <span class="n">rel_obj_id</span> <span class="o">=</span> <span class="n">rel_getter</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">rel_obj</span> <span class="o">=</span> <span class="n">rel_objs</span><span class="p">[</span><span class="n">rel_obj_id</span><span class="p">]</span>
                <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                    <span class="k">pass</span>  <span class="c1"># May happen in qs1 | qs2 scenarios.</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">rel_obj</span><span class="p">)</span>

            <span class="k">yield</span> <span class="n">obj</span>


<span class="k">class</span> <span class="nc">ValuesIterable</span><span class="p">(</span><span class="n">BaseIterable</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Iterable returned by QuerySet.values() that yields a dict for each row.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span>
        <span class="n">query</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">query</span>
        <span class="n">compiler</span> <span class="o">=</span> <span class="n">query</span><span class="o">.</span><span class="n">get_compiler</span><span class="p">(</span><span class="n">queryset</span><span class="o">.</span><span class="n">db</span><span class="p">)</span>

        <span class="c1"># extra(select=...) cols are always at the start of the row.</span>
        <span class="n">names</span> <span class="o">=</span> <span class="p">[</span>
            <span class="o">*</span><span class="n">query</span><span class="o">.</span><span class="n">extra_select</span><span class="p">,</span>
            <span class="o">*</span><span class="n">query</span><span class="o">.</span><span class="n">values_select</span><span class="p">,</span>
            <span class="o">*</span><span class="n">query</span><span class="o">.</span><span class="n">annotation_select</span><span class="p">,</span>
        <span class="p">]</span>
        <span class="n">indexes</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">names</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">compiler</span><span class="o">.</span><span class="n">results_iter</span><span class="p">(</span><span class="n">chunked_fetch</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">chunked_fetch</span><span class="p">,</span> <span class="n">chunk_size</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">chunk_size</span><span class="p">):</span>
            <span class="k">yield</span> <span class="p">{</span><span class="n">names</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span> <span class="n">row</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indexes</span><span class="p">}</span>


<span class="k">class</span> <span class="nc">ValuesListIterable</span><span class="p">(</span><span class="n">BaseIterable</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Iterable returned by QuerySet.values_list(flat=False) that yields a tuple</span>
<span class="sd">    for each row.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span>
        <span class="n">query</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">query</span>
        <span class="n">compiler</span> <span class="o">=</span> <span class="n">query</span><span class="o">.</span><span class="n">get_compiler</span><span class="p">(</span><span class="n">queryset</span><span class="o">.</span><span class="n">db</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">queryset</span><span class="o">.</span><span class="n">_fields</span><span class="p">:</span>
            <span class="c1"># extra(select=...) cols are always at the start of the row.</span>
            <span class="n">names</span> <span class="o">=</span> <span class="p">[</span>
                <span class="o">*</span><span class="n">query</span><span class="o">.</span><span class="n">extra_select</span><span class="p">,</span>
                <span class="o">*</span><span class="n">query</span><span class="o">.</span><span class="n">values_select</span><span class="p">,</span>
                <span class="o">*</span><span class="n">query</span><span class="o">.</span><span class="n">annotation_select</span><span class="p">,</span>
            <span class="p">]</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="o">*</span><span class="n">queryset</span><span class="o">.</span><span class="n">_fields</span><span class="p">,</span> <span class="o">*</span><span class="p">(</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">query</span><span class="o">.</span><span class="n">annotation_select</span> <span class="k">if</span> <span class="n">f</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">queryset</span><span class="o">.</span><span class="n">_fields</span><span class="p">)]</span>
            <span class="k">if</span> <span class="n">fields</span> <span class="o">!=</span> <span class="n">names</span><span class="p">:</span>
                <span class="c1"># Reorder according to fields.</span>
                <span class="n">index_map</span> <span class="o">=</span> <span class="p">{</span><span class="n">name</span><span class="p">:</span> <span class="n">idx</span> <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">names</span><span class="p">)}</span>
                <span class="n">rowfactory</span> <span class="o">=</span> <span class="n">operator</span><span class="o">.</span><span class="n">itemgetter</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="n">index_map</span><span class="p">[</span><span class="n">f</span><span class="p">]</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">])</span>
                <span class="k">return</span> <span class="nb">map</span><span class="p">(</span>
                    <span class="n">rowfactory</span><span class="p">,</span>
                    <span class="n">compiler</span><span class="o">.</span><span class="n">results_iter</span><span class="p">(</span><span class="n">chunked_fetch</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">chunked_fetch</span><span class="p">,</span> <span class="n">chunk_size</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">chunk_size</span><span class="p">)</span>
                <span class="p">)</span>
        <span class="k">return</span> <span class="n">compiler</span><span class="o">.</span><span class="n">results_iter</span><span class="p">(</span><span class="n">tuple_expected</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">chunked_fetch</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">chunked_fetch</span><span class="p">,</span> <span class="n">chunk_size</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">chunk_size</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">NamedValuesListIterable</span><span class="p">(</span><span class="n">ValuesListIterable</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Iterable returned by QuerySet.values_list(named=True) that yields a</span>
<span class="sd">    namedtuple for each row.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="nd">@staticmethod</span>
    <span class="nd">@lru_cache</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">create_namedtuple_class</span><span class="p">(</span><span class="o">*</span><span class="n">names</span><span class="p">):</span>
        <span class="c1"># Cache namedtuple() with @lru_cache() since it&#39;s too slow to be</span>
        <span class="c1"># called for every QuerySet evaluation.</span>
        <span class="k">return</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;Row&#39;</span><span class="p">,</span> <span class="n">names</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span>
        <span class="k">if</span> <span class="n">queryset</span><span class="o">.</span><span class="n">_fields</span><span class="p">:</span>
            <span class="n">names</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">_fields</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">query</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">query</span>
            <span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="o">*</span><span class="n">query</span><span class="o">.</span><span class="n">extra_select</span><span class="p">,</span> <span class="o">*</span><span class="n">query</span><span class="o">.</span><span class="n">values_select</span><span class="p">,</span> <span class="o">*</span><span class="n">query</span><span class="o">.</span><span class="n">annotation_select</span><span class="p">]</span>
        <span class="n">tuple_class</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create_namedtuple_class</span><span class="p">(</span><span class="o">*</span><span class="n">names</span><span class="p">)</span>
        <span class="n">new</span> <span class="o">=</span> <span class="nb">tuple</span><span class="o">.</span><span class="fm">__new__</span>
        <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__iter__</span><span class="p">():</span>
            <span class="k">yield</span> <span class="n">new</span><span class="p">(</span><span class="n">tuple_class</span><span class="p">,</span> <span class="n">row</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">FlatValuesListIterable</span><span class="p">(</span><span class="n">BaseIterable</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Iterable returned by QuerySet.values_list(flat=True) that yields single</span>
<span class="sd">    values.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">queryset</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span>
        <span class="n">compiler</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">get_compiler</span><span class="p">(</span><span class="n">queryset</span><span class="o">.</span><span class="n">db</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">compiler</span><span class="o">.</span><span class="n">results_iter</span><span class="p">(</span><span class="n">chunked_fetch</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">chunked_fetch</span><span class="p">,</span> <span class="n">chunk_size</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">chunk_size</span><span class="p">):</span>
            <span class="k">yield</span> <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>


<div class="viewcode-block" id="QuerySet"><a class="viewcode-back" href="../../../../ref/models/querysets.html#django.db.models.query.QuerySet">[docs]</a><span class="k">class</span> <span class="nc">QuerySet</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Represent a lazy database lookup for a set of objects.&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">query</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">hints</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_db</span> <span class="o">=</span> <span class="n">using</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_hints</span> <span class="o">=</span> <span class="n">hints</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">query</span> <span class="o">=</span> <span class="n">query</span> <span class="ow">or</span> <span class="n">sql</span><span class="o">.</span><span class="n">Query</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sticky_filter</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_for_write</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="o">=</span> <span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_done</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_known_related_objects</span> <span class="o">=</span> <span class="p">{}</span>  <span class="c1"># {rel_field: {pk: rel_obj}}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_iterable_class</span> <span class="o">=</span> <span class="n">ModelIterable</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fields</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">as_manager</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span>
        <span class="c1"># Address the circular dependency between `Queryset` and `Manager`.</span>
        <span class="kn">from</span> <span class="nn">django.db.models.manager</span> <span class="k">import</span> <span class="n">Manager</span>
        <span class="n">manager</span> <span class="o">=</span> <span class="n">Manager</span><span class="o">.</span><span class="n">from_queryset</span><span class="p">(</span><span class="bp">cls</span><span class="p">)()</span>
        <span class="n">manager</span><span class="o">.</span><span class="n">_built_with_as_manager</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="n">manager</span>
    <span class="n">as_manager</span><span class="o">.</span><span class="n">queryset_only</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">as_manager</span> <span class="o">=</span> <span class="nb">classmethod</span><span class="p">(</span><span class="n">as_manager</span><span class="p">)</span>

    <span class="c1">########################</span>
    <span class="c1"># PYTHON MAGIC METHODS #</span>
    <span class="c1">########################</span>

    <span class="k">def</span> <span class="nf">__deepcopy__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">memo</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Don&#39;t populate the QuerySet&#39;s cache.&quot;&quot;&quot;</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">()</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">k</span> <span class="o">==</span> <span class="s1">&#39;_result_cache&#39;</span><span class="p">:</span>
                <span class="n">obj</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">obj</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">memo</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">__getstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Force the cache to be fully populated.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fetch_all</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">{</span><span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">,</span> <span class="n">DJANGO_VERSION_PICKLE_KEY</span><span class="p">:</span> <span class="n">get_version</span><span class="p">()}</span>

    <span class="k">def</span> <span class="nf">__setstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">state</span><span class="p">):</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">pickled_version</span> <span class="o">=</span> <span class="n">state</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">DJANGO_VERSION_PICKLE_KEY</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">pickled_version</span><span class="p">:</span>
            <span class="n">current_version</span> <span class="o">=</span> <span class="n">get_version</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">current_version</span> <span class="o">!=</span> <span class="n">pickled_version</span><span class="p">:</span>
                <span class="n">msg</span> <span class="o">=</span> <span class="p">(</span>
                    <span class="s2">&quot;Pickled queryset instance&#39;s Django version </span><span class="si">%s</span><span class="s2"> does not &quot;</span>
                    <span class="s2">&quot;match the current version </span><span class="si">%s</span><span class="s2">.&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">pickled_version</span><span class="p">,</span> <span class="n">current_version</span><span class="p">)</span>
                <span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;Pickled queryset instance&#39;s Django version is not specified.&quot;</span>

        <span class="k">if</span> <span class="n">msg</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="ne">RuntimeWarning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">state</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="p">[:</span><span class="n">REPR_OUTPUT_SIZE</span> <span class="o">+</span> <span class="mi">1</span><span class="p">])</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">REPR_OUTPUT_SIZE</span><span class="p">:</span>
            <span class="n">data</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;...(remaining elements truncated)...&quot;</span>
        <span class="k">return</span> <span class="s1">&#39;&lt;</span><span class="si">%s</span><span class="s1"> </span><span class="si">%r</span><span class="s1">&gt;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fetch_all</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        The queryset iterator protocol uses three nested iterators in the</span>
<span class="sd">        default case:</span>
<span class="sd">            1. sql.compiler.execute_sql()</span>
<span class="sd">               - Returns 100 rows at time (constants.GET_ITERATOR_CHUNK_SIZE)</span>
<span class="sd">                 using cursor.fetchmany(). This part is responsible for</span>
<span class="sd">                 doing some column masking, and returning the rows in chunks.</span>
<span class="sd">            2. sql.compiler.results_iter()</span>
<span class="sd">               - Returns one row at time. At this point the rows are still just</span>
<span class="sd">                 tuples. In some cases the return values are converted to</span>
<span class="sd">                 Python values at this location.</span>
<span class="sd">            3. self.iterator()</span>
<span class="sd">               - Responsible for turning the rows into model objects.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fetch_all</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__bool__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fetch_all</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Retrieve an item or slice from the set of results.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">slice</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span>
        <span class="k">assert</span> <span class="p">((</span><span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="nb">slice</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">k</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">))</span> <span class="ow">or</span>
                <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="nb">slice</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">k</span><span class="o">.</span><span class="n">start</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">k</span><span class="o">.</span><span class="n">start</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span> <span class="ow">and</span>
                 <span class="p">(</span><span class="n">k</span><span class="o">.</span><span class="n">stop</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">k</span><span class="o">.</span><span class="n">stop</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">))),</span> \
            <span class="s2">&quot;Negative indexing is not supported.&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span><span class="p">[</span><span class="n">k</span><span class="p">]</span>

        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="nb">slice</span><span class="p">):</span>
            <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">k</span><span class="o">.</span><span class="n">start</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">start</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">k</span><span class="o">.</span><span class="n">start</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">start</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="n">k</span><span class="o">.</span><span class="n">stop</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">stop</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">k</span><span class="o">.</span><span class="n">stop</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">stop</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="n">qs</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">set_limits</span><span class="p">(</span><span class="n">start</span><span class="p">,</span> <span class="n">stop</span><span class="p">)</span>
            <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">qs</span><span class="p">)[::</span><span class="n">k</span><span class="o">.</span><span class="n">step</span><span class="p">]</span> <span class="k">if</span> <span class="n">k</span><span class="o">.</span><span class="n">step</span> <span class="k">else</span> <span class="n">qs</span>

        <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="n">qs</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">set_limits</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">qs</span><span class="o">.</span><span class="n">_fetch_all</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">qs</span><span class="o">.</span><span class="n">_result_cache</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__and__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_merge_sanity_check</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">EmptyQuerySet</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">other</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">EmptyQuerySet</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="n">combined</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="n">combined</span><span class="o">.</span><span class="n">_merge_known_related_objects</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
        <span class="n">combined</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">combine</span><span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">query</span><span class="p">,</span> <span class="n">sql</span><span class="o">.</span><span class="n">AND</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">combined</span>

    <span class="k">def</span> <span class="nf">__or__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_merge_sanity_check</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">EmptyQuerySet</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">other</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">EmptyQuerySet</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="n">query</span> <span class="o">=</span> <span class="bp">self</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">can_filter</span><span class="p">()</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_base_manager</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">pk__in</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="s1">&#39;pk&#39;</span><span class="p">))</span>
        <span class="n">combined</span> <span class="o">=</span> <span class="n">query</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="n">combined</span><span class="o">.</span><span class="n">_merge_known_related_objects</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">other</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">can_filter</span><span class="p">():</span>
            <span class="n">other</span> <span class="o">=</span> <span class="n">other</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_base_manager</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">pk__in</span><span class="o">=</span><span class="n">other</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="s1">&#39;pk&#39;</span><span class="p">))</span>
        <span class="n">combined</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">combine</span><span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">query</span><span class="p">,</span> <span class="n">sql</span><span class="o">.</span><span class="n">OR</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">combined</span>

    <span class="c1">####################################</span>
    <span class="c1"># METHODS THAT DO DATABASE QUERIES #</span>
    <span class="c1">####################################</span>

    <span class="k">def</span> <span class="nf">_iterator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">use_chunked_fetch</span><span class="p">,</span> <span class="n">chunk_size</span><span class="p">):</span>
        <span class="k">yield from</span> <span class="bp">self</span><span class="o">.</span><span class="n">_iterable_class</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">chunked_fetch</span><span class="o">=</span><span class="n">use_chunked_fetch</span><span class="p">,</span> <span class="n">chunk_size</span><span class="o">=</span><span class="n">chunk_size</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">iterator</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">chunk_size</span><span class="o">=</span><span class="mi">2000</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        An iterator over the results from applying this QuerySet to the</span>
<span class="sd">        database.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">chunk_size</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Chunk size must be strictly positive.&#39;</span><span class="p">)</span>
        <span class="n">use_chunked_fetch</span> <span class="o">=</span> <span class="ow">not</span> <span class="n">connections</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">]</span><span class="o">.</span><span class="n">settings_dict</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;DISABLE_SERVER_SIDE_CURSORS&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_iterator</span><span class="p">(</span><span class="n">use_chunked_fetch</span><span class="p">,</span> <span class="n">chunk_size</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">aggregate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a dictionary containing the calculations (aggregation)</span>
<span class="sd">        over the current queryset.</span>

<span class="sd">        If args is present the expression is passed as a kwarg using</span>
<span class="sd">        the Aggregate object&#39;s default alias.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">distinct_fields</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;aggregate() + distinct(fields) not implemented.&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_validate_values_are_expressions</span><span class="p">((</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">*</span><span class="n">kwargs</span><span class="o">.</span><span class="n">values</span><span class="p">()),</span> <span class="n">method_name</span><span class="o">=</span><span class="s1">&#39;aggregate&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span>
            <span class="c1"># The default_alias property raises TypeError if default_alias</span>
            <span class="c1"># can&#39;t be set automatically or AttributeError if it isn&#39;t an</span>
            <span class="c1"># attribute.</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">arg</span><span class="o">.</span><span class="n">default_alias</span>
            <span class="k">except</span> <span class="p">(</span><span class="ne">AttributeError</span><span class="p">,</span> <span class="ne">TypeError</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Complex aggregates require an alias&quot;</span><span class="p">)</span>
            <span class="n">kwargs</span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">default_alias</span><span class="p">]</span> <span class="o">=</span> <span class="n">arg</span>

        <span class="n">query</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">chain</span><span class="p">()</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">alias</span><span class="p">,</span> <span class="n">aggregate_expr</span><span class="p">)</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="n">query</span><span class="o">.</span><span class="n">add_annotation</span><span class="p">(</span><span class="n">aggregate_expr</span><span class="p">,</span> <span class="n">alias</span><span class="p">,</span> <span class="n">is_summary</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">query</span><span class="o">.</span><span class="n">annotations</span><span class="p">[</span><span class="n">alias</span><span class="p">]</span><span class="o">.</span><span class="n">contains_aggregate</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> is not an aggregate expression&quot;</span> <span class="o">%</span> <span class="n">alias</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">query</span><span class="o">.</span><span class="n">get_aggregation</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">count</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Perform a SELECT COUNT() and return the number of records as an</span>
<span class="sd">        integer.</span>

<span class="sd">        If the QuerySet is already fully cached, return the length of the</span>
<span class="sd">        cached results set to avoid multiple SELECT COUNT(*) calls.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">get_count</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Perform the query and return a single object matching the given</span>
<span class="sd">        keyword arguments.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">can_filter</span><span class="p">()</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">distinct_fields</span><span class="p">:</span>
            <span class="n">clone</span> <span class="o">=</span> <span class="n">clone</span><span class="o">.</span><span class="n">order_by</span><span class="p">()</span>
        <span class="n">num</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">clone</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">num</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">clone</span><span class="o">.</span><span class="n">_result_cache</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">num</span><span class="p">:</span>
            <span class="k">raise</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">(</span>
                <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> matching query does not exist.&quot;</span> <span class="o">%</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span>
            <span class="p">)</span>
        <span class="k">raise</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">MultipleObjectsReturned</span><span class="p">(</span>
            <span class="s2">&quot;get() returned more than one </span><span class="si">%s</span><span class="s2"> -- it returned </span><span class="si">%s</span><span class="s2">!&quot;</span> <span class="o">%</span>
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span> <span class="n">num</span><span class="p">)</span>
        <span class="p">)</span>

    <span class="k">def</span> <span class="nf">create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Create a new object with the given kwargs, saving it to the database</span>
<span class="sd">        and returning the created object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_for_write</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">force_insert</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">_populate_pk_values</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">objs</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">objs</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">obj</span><span class="o">.</span><span class="n">pk</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">obj</span><span class="o">.</span><span class="n">pk</span> <span class="o">=</span> <span class="n">obj</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">get_pk_value_on_save</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">bulk_create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">objs</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">ignore_conflicts</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Insert each of the instances into the database. Do *not* call</span>
<span class="sd">        save() on each of the instances, do not send any pre/post_save</span>
<span class="sd">        signals, and do not set the primary key attribute if it is an</span>
<span class="sd">        autoincrement field (except if features.can_return_ids_from_bulk_insert=True).</span>
<span class="sd">        Multi-table models are not supported.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># When you bulk insert you don&#39;t get the primary keys back (if it&#39;s an</span>
        <span class="c1"># autoincrement, except if can_return_ids_from_bulk_insert=True), so</span>
        <span class="c1"># you can&#39;t insert into the child tables which references this. There</span>
        <span class="c1"># are two workarounds:</span>
        <span class="c1"># 1) This could be implemented if you didn&#39;t have an autoincrement pk</span>
        <span class="c1"># 2) You could do it by doing O(n) normal inserts into the parent</span>
        <span class="c1">#    tables to get the primary keys back and then doing a single bulk</span>
        <span class="c1">#    insert into the childmost table.</span>
        <span class="c1"># We currently set the primary keys on the objects when using</span>
        <span class="c1"># PostgreSQL via the RETURNING ID clause. It should be possible for</span>
        <span class="c1"># Oracle as well, but the semantics for extracting the primary keys is</span>
        <span class="c1"># trickier so it&#39;s not done yet.</span>
        <span class="k">assert</span> <span class="n">batch_size</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">batch_size</span> <span class="o">&gt;</span> <span class="mi">0</span>
        <span class="c1"># Check that the parents share the same concrete model with the our</span>
        <span class="c1"># model to detect the inheritance pattern ConcreteGrandParent -&gt;</span>
        <span class="c1"># MultiTableParent -&gt; ProxyChild. Simply checking self.model._meta.proxy</span>
        <span class="c1"># would not identify that case as involving multiple tables.</span>
        <span class="k">for</span> <span class="n">parent</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_parent_list</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">parent</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_model</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Can&#39;t bulk create a multi-table inherited model&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">objs</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">objs</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_for_write</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">connection</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">]</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_fields</span>
        <span class="n">objs</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">objs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_populate_pk_values</span><span class="p">(</span><span class="n">objs</span><span class="p">)</span>
        <span class="k">with</span> <span class="n">transaction</span><span class="o">.</span><span class="n">atomic</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="n">savepoint</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
            <span class="n">objs_with_pk</span><span class="p">,</span> <span class="n">objs_without_pk</span> <span class="o">=</span> <span class="n">partition</span><span class="p">(</span><span class="k">lambda</span> <span class="n">o</span><span class="p">:</span> <span class="n">o</span><span class="o">.</span><span class="n">pk</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">,</span> <span class="n">objs</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">objs_with_pk</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_batched_insert</span><span class="p">(</span><span class="n">objs_with_pk</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">,</span> <span class="n">ignore_conflicts</span><span class="o">=</span><span class="n">ignore_conflicts</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">obj_with_pk</span> <span class="ow">in</span> <span class="n">objs_with_pk</span><span class="p">:</span>
                    <span class="n">obj_with_pk</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">adding</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="n">obj_with_pk</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span>
            <span class="k">if</span> <span class="n">objs_without_pk</span><span class="p">:</span>
                <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">AutoField</span><span class="p">)]</span>
                <span class="n">ids</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_batched_insert</span><span class="p">(</span><span class="n">objs_without_pk</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">,</span> <span class="n">ignore_conflicts</span><span class="o">=</span><span class="n">ignore_conflicts</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">connection</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">can_return_ids_from_bulk_insert</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">ignore_conflicts</span><span class="p">:</span>
                    <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">ids</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">objs_without_pk</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">obj_without_pk</span><span class="p">,</span> <span class="n">pk</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">objs_without_pk</span><span class="p">,</span> <span class="n">ids</span><span class="p">):</span>
                    <span class="n">obj_without_pk</span><span class="o">.</span><span class="n">pk</span> <span class="o">=</span> <span class="n">pk</span>
                    <span class="n">obj_without_pk</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">adding</span> <span class="o">=</span> <span class="kc">False</span>
                    <span class="n">obj_without_pk</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span>

        <span class="k">return</span> <span class="n">objs</span>

    <span class="k">def</span> <span class="nf">bulk_update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">objs</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Update the given fields in each of the given objects in the database.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">batch_size</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">batch_size</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Batch size must be a positive integer.&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">fields</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Field names must be given to bulk_update().&#39;</span><span class="p">)</span>
        <span class="n">objs</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">objs</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">pk</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">objs</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;All bulk_update() objects must have a primary key set.&#39;</span><span class="p">)</span>
        <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">]</span>
        <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="ow">not</span> <span class="n">f</span><span class="o">.</span><span class="n">concrete</span> <span class="ow">or</span> <span class="n">f</span><span class="o">.</span><span class="n">many_to_many</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;bulk_update() can only be used with concrete fields.&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">any</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">primary_key</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;bulk_update() cannot be used with primary key fields.&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">objs</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="c1"># PK is used twice in the resulting update query, once in the filter</span>
        <span class="c1"># and once in the WHEN. Each field will also have one CAST.</span>
        <span class="n">max_batch_size</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">]</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">bulk_batch_size</span><span class="p">([</span><span class="s1">&#39;pk&#39;</span><span class="p">,</span> <span class="s1">&#39;pk&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">fields</span><span class="p">,</span> <span class="n">objs</span><span class="p">)</span>
        <span class="n">batch_size</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">batch_size</span><span class="p">,</span> <span class="n">max_batch_size</span><span class="p">)</span> <span class="k">if</span> <span class="n">batch_size</span> <span class="k">else</span> <span class="n">max_batch_size</span>
        <span class="n">requires_casting</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">]</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">requires_casted_case_in_updates</span>
        <span class="n">batches</span> <span class="o">=</span> <span class="p">(</span><span class="n">objs</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">i</span> <span class="o">+</span> <span class="n">batch_size</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">objs</span><span class="p">),</span> <span class="n">batch_size</span><span class="p">))</span>
        <span class="n">updates</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">batch_objs</span> <span class="ow">in</span> <span class="n">batches</span><span class="p">:</span>
            <span class="n">update_kwargs</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
                <span class="n">when_statements</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">batch_objs</span><span class="p">:</span>
                    <span class="n">attr</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attr</span><span class="p">,</span> <span class="n">Expression</span><span class="p">):</span>
                        <span class="n">attr</span> <span class="o">=</span> <span class="n">Value</span><span class="p">(</span><span class="n">attr</span><span class="p">,</span> <span class="n">output_field</span><span class="o">=</span><span class="n">field</span><span class="p">)</span>
                    <span class="n">when_statements</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">When</span><span class="p">(</span><span class="n">pk</span><span class="o">=</span><span class="n">obj</span><span class="o">.</span><span class="n">pk</span><span class="p">,</span> <span class="n">then</span><span class="o">=</span><span class="n">attr</span><span class="p">))</span>
                <span class="n">case_statement</span> <span class="o">=</span> <span class="n">Case</span><span class="p">(</span><span class="o">*</span><span class="n">when_statements</span><span class="p">,</span> <span class="n">output_field</span><span class="o">=</span><span class="n">field</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">requires_casting</span><span class="p">:</span>
                    <span class="n">case_statement</span> <span class="o">=</span> <span class="n">Cast</span><span class="p">(</span><span class="n">case_statement</span><span class="p">,</span> <span class="n">output_field</span><span class="o">=</span><span class="n">field</span><span class="p">)</span>
                <span class="n">update_kwargs</span><span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">]</span> <span class="o">=</span> <span class="n">case_statement</span>
            <span class="n">updates</span><span class="o">.</span><span class="n">append</span><span class="p">(([</span><span class="n">obj</span><span class="o">.</span><span class="n">pk</span> <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">batch_objs</span><span class="p">],</span> <span class="n">update_kwargs</span><span class="p">))</span>
        <span class="k">with</span> <span class="n">transaction</span><span class="o">.</span><span class="n">atomic</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="n">savepoint</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">pks</span><span class="p">,</span> <span class="n">update_kwargs</span> <span class="ow">in</span> <span class="n">updates</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">pk__in</span><span class="o">=</span><span class="n">pks</span><span class="p">)</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="o">**</span><span class="n">update_kwargs</span><span class="p">)</span>
    <span class="n">bulk_update</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">get_or_create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">defaults</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Look up an object with the given kwargs, creating one if necessary.</span>
<span class="sd">        Return a tuple of (object, created), where created is a boolean</span>
<span class="sd">        specifying whether an object was created.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="c1"># The get() needs to be targeted at the write database in order</span>
        <span class="c1"># to avoid potential transaction consistency problems.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_for_write</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">),</span> <span class="kc">False</span>
        <span class="k">except</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">:</span>
            <span class="n">params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extract_model_params</span><span class="p">(</span><span class="n">defaults</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_object_from_params</span><span class="p">(</span><span class="n">kwargs</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">update_or_create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">defaults</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Look up an object with the given kwargs, updating one with defaults</span>
<span class="sd">        if it exists, otherwise create a new one.</span>
<span class="sd">        Return a tuple (object, created), where created is a boolean</span>
<span class="sd">        specifying whether an object was created.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="n">defaults</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_for_write</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">with</span> <span class="n">transaction</span><span class="o">.</span><span class="n">atomic</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">select_for_update</span><span class="p">()</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">except</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">:</span>
                <span class="n">params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_extract_model_params</span><span class="p">(</span><span class="n">defaults</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
                <span class="c1"># Lock the row so that a concurrent update is blocked until</span>
                <span class="c1"># after update_or_create() has performed its save.</span>
                <span class="n">obj</span><span class="p">,</span> <span class="n">created</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_create_object_from_params</span><span class="p">(</span><span class="n">kwargs</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">lock</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">created</span><span class="p">:</span>
                    <span class="k">return</span> <span class="n">obj</span><span class="p">,</span> <span class="n">created</span>
            <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">defaults</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">()</span> <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">else</span> <span class="n">v</span><span class="p">)</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span><span class="p">,</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">_create_object_from_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lookup</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">lock</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Try to create an object using passed params. Used by get_or_create()</span>
<span class="sd">        and update_or_create().</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">with</span> <span class="n">transaction</span><span class="o">.</span><span class="n">atomic</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">):</span>
                <span class="n">params</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="n">v</span><span class="p">()</span> <span class="k">if</span> <span class="n">callable</span><span class="p">(</span><span class="n">v</span><span class="p">)</span> <span class="k">else</span> <span class="n">v</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">params</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
                <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="o">**</span><span class="n">params</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">obj</span><span class="p">,</span> <span class="kc">True</span>
        <span class="k">except</span> <span class="n">IntegrityError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">select_for_update</span><span class="p">()</span> <span class="k">if</span> <span class="n">lock</span> <span class="k">else</span> <span class="bp">self</span>
                <span class="k">return</span> <span class="n">qs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="o">**</span><span class="n">lookup</span><span class="p">),</span> <span class="kc">False</span>
            <span class="k">except</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">DoesNotExist</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="k">raise</span> <span class="n">e</span>

    <span class="k">def</span> <span class="nf">_extract_model_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">defaults</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Prepare `params` for creating a model instance based on the given</span>
<span class="sd">        kwargs; for use by get_or_create() and update_or_create().</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">defaults</span> <span class="o">=</span> <span class="n">defaults</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="n">params</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="n">v</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">LOOKUP_SEP</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">k</span><span class="p">}</span>
        <span class="n">params</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">defaults</span><span class="p">)</span>
        <span class="n">property_names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">_property_names</span>
        <span class="n">invalid_params</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">params</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">param</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">FieldDoesNotExist</span><span class="p">:</span>
                <span class="c1"># It&#39;s okay to use a model&#39;s property if it has a setter.</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">param</span> <span class="ow">in</span> <span class="n">property_names</span> <span class="ow">and</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">param</span><span class="p">)</span><span class="o">.</span><span class="n">fset</span><span class="p">):</span>
                    <span class="n">invalid_params</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">param</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">invalid_params</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">FieldError</span><span class="p">(</span>
                <span class="s2">&quot;Invalid field name(s) for model </span><span class="si">%s</span><span class="s2">: &#39;</span><span class="si">%s</span><span class="s2">&#39;.&quot;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">object_name</span><span class="p">,</span>
                    <span class="s2">&quot;&#39;, &#39;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">invalid_params</span><span class="p">)),</span>
                <span class="p">))</span>
        <span class="k">return</span> <span class="n">params</span>

    <span class="k">def</span> <span class="nf">_earliest</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">fields</span><span class="p">,</span> <span class="n">field_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the earliest object according to fields (if given) or by the</span>
<span class="sd">        model&#39;s Meta.get_latest_by.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">fields</span> <span class="ow">and</span> <span class="n">field_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Cannot use both positional arguments and the field_name keyword argument.&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">field_name</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
                <span class="s1">&#39;The field_name keyword argument to earliest() and latest() &#39;</span>
                <span class="s1">&#39;is deprecated in favor of passing positional arguments.&#39;</span><span class="p">,</span>
                <span class="n">RemovedInDjango30Warning</span><span class="p">,</span>
            <span class="p">)</span>
            <span class="n">order_by</span> <span class="o">=</span> <span class="p">(</span><span class="n">field_name</span><span class="p">,)</span>
        <span class="k">elif</span> <span class="n">fields</span><span class="p">:</span>
            <span class="n">order_by</span> <span class="o">=</span> <span class="n">fields</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">order_by</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="p">,</span> <span class="s1">&#39;get_latest_by&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">order_by</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">order_by</span><span class="p">,</span> <span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">list</span><span class="p">)):</span>
                <span class="n">order_by</span> <span class="o">=</span> <span class="p">(</span><span class="n">order_by</span><span class="p">,)</span>
        <span class="k">if</span> <span class="n">order_by</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;earliest() and latest() require either fields as positional &quot;</span>
                <span class="s2">&quot;arguments or &#39;get_latest_by&#39; in the model&#39;s Meta.&quot;</span>
            <span class="p">)</span>

        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">can_filter</span><span class="p">(),</span> \
            <span class="s2">&quot;Cannot change a query once a slice has been taken.&quot;</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">set_limits</span><span class="p">(</span><span class="n">high</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">clear_ordering</span><span class="p">(</span><span class="n">force_empty</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">add_ordering</span><span class="p">(</span><span class="o">*</span><span class="n">order_by</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">earliest</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">fields</span><span class="p">,</span> <span class="n">field_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_earliest</span><span class="p">(</span><span class="o">*</span><span class="n">fields</span><span class="p">,</span> <span class="n">field_name</span><span class="o">=</span><span class="n">field_name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">latest</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">fields</span><span class="p">,</span> <span class="n">field_name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span><span class="o">.</span><span class="n">_earliest</span><span class="p">(</span><span class="o">*</span><span class="n">fields</span><span class="p">,</span> <span class="n">field_name</span><span class="o">=</span><span class="n">field_name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">first</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the first object of a query or None if no match is found.&quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="p">(</span><span class="bp">self</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ordered</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="s1">&#39;pk&#39;</span><span class="p">))[:</span><span class="mi">1</span><span class="p">]:</span>
            <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">last</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the last object of a query or None if no match is found.&quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span> <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ordered</span> <span class="k">else</span> <span class="bp">self</span><span class="o">.</span><span class="n">order_by</span><span class="p">(</span><span class="s1">&#39;-pk&#39;</span><span class="p">))[:</span><span class="mi">1</span><span class="p">]:</span>
            <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">in_bulk</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">id_list</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">field_name</span><span class="o">=</span><span class="s1">&#39;pk&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a dictionary mapping each of the given IDs to the object with</span>
<span class="sd">        that ID. If `id_list` isn&#39;t provided, evaluate the entire QuerySet.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">can_filter</span><span class="p">(),</span> \
            <span class="s2">&quot;Cannot use &#39;limit&#39; or &#39;offset&#39; with in_bulk&quot;</span>
        <span class="k">if</span> <span class="n">field_name</span> <span class="o">!=</span> <span class="s1">&#39;pk&#39;</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">field_name</span><span class="p">)</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;in_bulk()&#39;s field_name must be a unique field but </span><span class="si">%r</span><span class="s2"> isn&#39;t.&quot;</span> <span class="o">%</span> <span class="n">field_name</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">id_list</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">id_list</span><span class="p">:</span>
                <span class="k">return</span> <span class="p">{}</span>
            <span class="n">filter_key</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">{}</span><span class="s1">__in&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">field_name</span><span class="p">)</span>
            <span class="n">batch_size</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">]</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">max_query_params</span>
            <span class="n">id_list</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">id_list</span><span class="p">)</span>
            <span class="c1"># If the database has a limit on the number of query parameters</span>
            <span class="c1"># (e.g. SQLite), retrieve objects in batches if necessary.</span>
            <span class="k">if</span> <span class="n">batch_size</span> <span class="ow">and</span> <span class="n">batch_size</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">id_list</span><span class="p">):</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="p">()</span>
                <span class="k">for</span> <span class="n">offset</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">id_list</span><span class="p">),</span> <span class="n">batch_size</span><span class="p">):</span>
                    <span class="n">batch</span> <span class="o">=</span> <span class="n">id_list</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span> <span class="o">+</span> <span class="n">batch_size</span><span class="p">]</span>
                    <span class="n">qs</span> <span class="o">+=</span> <span class="nb">tuple</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="n">filter_key</span><span class="p">:</span> <span class="n">batch</span><span class="p">})</span><span class="o">.</span><span class="n">order_by</span><span class="p">())</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="o">**</span><span class="p">{</span><span class="n">filter_key</span><span class="p">:</span> <span class="n">id_list</span><span class="p">})</span><span class="o">.</span><span class="n">order_by</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">qs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">{</span><span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">field_name</span><span class="p">):</span> <span class="n">obj</span> <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">qs</span><span class="p">}</span>

    <span class="k">def</span> <span class="nf">delete</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Delete the records in the current QuerySet.&quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">can_filter</span><span class="p">(),</span> \
            <span class="s2">&quot;Cannot use &#39;limit&#39; or &#39;offset&#39; with delete.&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Cannot call delete() after .values() or .values_list()&quot;</span><span class="p">)</span>

        <span class="n">del_query</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>

        <span class="c1"># The delete is actually 2 queries - one to find related objects,</span>
        <span class="c1"># and one to delete. Make sure that the discovery of related</span>
        <span class="c1"># objects is performed on the same database as the deletion.</span>
        <span class="n">del_query</span><span class="o">.</span><span class="n">_for_write</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="c1"># Disable non-supported fields.</span>
        <span class="n">del_query</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">select_for_update</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">del_query</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">select_related</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">del_query</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">clear_ordering</span><span class="p">(</span><span class="n">force_empty</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>

        <span class="n">collector</span> <span class="o">=</span> <span class="n">Collector</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">del_query</span><span class="o">.</span><span class="n">db</span><span class="p">)</span>
        <span class="n">collector</span><span class="o">.</span><span class="n">collect</span><span class="p">(</span><span class="n">del_query</span><span class="p">)</span>
        <span class="n">deleted</span><span class="p">,</span> <span class="n">_rows_count</span> <span class="o">=</span> <span class="n">collector</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span>

        <span class="c1"># Clear the result cache, in case this QuerySet gets reused.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">deleted</span><span class="p">,</span> <span class="n">_rows_count</span>

    <span class="n">delete</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">delete</span><span class="o">.</span><span class="n">queryset_only</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_raw_delete</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">using</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Delete objects found from the given queryset in single direct SQL</span>
<span class="sd">        query. No signals are sent and there is no protection for cascades.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">sql</span><span class="o">.</span><span class="n">DeleteQuery</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span><span class="o">.</span><span class="n">delete_qs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">using</span><span class="p">)</span>
    <span class="n">_raw_delete</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Update all elements in the current QuerySet, setting all the given</span>
<span class="sd">        fields to the appropriate values.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">can_filter</span><span class="p">(),</span> \
            <span class="s2">&quot;Cannot update a query once a slice has been taken.&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_for_write</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">query</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span><span class="n">sql</span><span class="o">.</span><span class="n">UpdateQuery</span><span class="p">)</span>
        <span class="n">query</span><span class="o">.</span><span class="n">add_update_values</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="c1"># Clear any annotations so that they won&#39;t be present in subqueries.</span>
        <span class="n">query</span><span class="o">.</span><span class="n">_annotations</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">with</span> <span class="n">transaction</span><span class="o">.</span><span class="n">mark_for_rollback_on_error</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">):</span>
            <span class="n">rows</span> <span class="o">=</span> <span class="n">query</span><span class="o">.</span><span class="n">get_compiler</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">)</span><span class="o">.</span><span class="n">execute_sql</span><span class="p">(</span><span class="n">CURSOR</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">rows</span>
    <span class="n">update</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">values</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        A version of update() that accepts field objects instead of field names.</span>
<span class="sd">        Used primarily for model saving and not intended for use by general</span>
<span class="sd">        code (it requires too much poking around at model internals to be</span>
<span class="sd">        useful at that level).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">can_filter</span><span class="p">(),</span> \
            <span class="s2">&quot;Cannot update a query once a slice has been taken.&quot;</span>
        <span class="n">query</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span><span class="n">sql</span><span class="o">.</span><span class="n">UpdateQuery</span><span class="p">)</span>
        <span class="n">query</span><span class="o">.</span><span class="n">add_update_fields</span><span class="p">(</span><span class="n">values</span><span class="p">)</span>
        <span class="c1"># Clear any annotations so that they won&#39;t be present in subqueries.</span>
        <span class="n">query</span><span class="o">.</span><span class="n">_annotations</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">query</span><span class="o">.</span><span class="n">get_compiler</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">)</span><span class="o">.</span><span class="n">execute_sql</span><span class="p">(</span><span class="n">CURSOR</span><span class="p">)</span>
    <span class="n">_update</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">_update</span><span class="o">.</span><span class="n">queryset_only</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">exists</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">has_results</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_prefetch_related_objects</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># This method can only be called once the result cache has been filled.</span>
        <span class="n">prefetch_related_objects</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span><span class="p">,</span> <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_done</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">explain</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">explain</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="nb">format</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">)</span>

    <span class="c1">##################################################</span>
    <span class="c1"># PUBLIC METHODS THAT RETURN A QUERYSET SUBCLASS #</span>
    <span class="c1">##################################################</span>

    <span class="k">def</span> <span class="nf">raw</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">raw_query</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">translations</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">using</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">using</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span>
        <span class="n">qs</span> <span class="o">=</span> <span class="n">RawQuerySet</span><span class="p">(</span><span class="n">raw_query</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">,</span> <span class="n">translations</span><span class="o">=</span><span class="n">translations</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span>
        <span class="n">qs</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span><span class="p">[:]</span>
        <span class="k">return</span> <span class="n">qs</span>

    <span class="k">def</span> <span class="nf">_values</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">fields</span><span class="p">,</span> <span class="o">**</span><span class="n">expressions</span><span class="p">):</span>
        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">expressions</span><span class="p">:</span>
            <span class="n">clone</span> <span class="o">=</span> <span class="n">clone</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span><span class="o">**</span><span class="n">expressions</span><span class="p">)</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">_fields</span> <span class="o">=</span> <span class="n">fields</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">set_values</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">clone</span>

    <span class="k">def</span> <span class="nf">values</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">fields</span><span class="p">,</span> <span class="o">**</span><span class="n">expressions</span><span class="p">):</span>
        <span class="n">fields</span> <span class="o">+=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">expressions</span><span class="p">)</span>
        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_values</span><span class="p">(</span><span class="o">*</span><span class="n">fields</span><span class="p">,</span> <span class="o">**</span><span class="n">expressions</span><span class="p">)</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">_iterable_class</span> <span class="o">=</span> <span class="n">ValuesIterable</span>
        <span class="k">return</span> <span class="n">clone</span>

    <span class="k">def</span> <span class="nf">values_list</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">fields</span><span class="p">,</span> <span class="n">flat</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">named</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">flat</span> <span class="ow">and</span> <span class="n">named</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;&#39;flat&#39; and &#39;named&#39; can&#39;t be used together.&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">flat</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;&#39;flat&#39; is not valid when values_list is called with more than one field.&quot;</span><span class="p">)</span>

        <span class="n">field_names</span> <span class="o">=</span> <span class="p">{</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span> <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s1">&#39;resolve_expression&#39;</span><span class="p">)}</span>
        <span class="n">_fields</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">expressions</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">counter</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="s1">&#39;resolve_expression&#39;</span><span class="p">):</span>
                <span class="n">field_id_prefix</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="s1">&#39;default_alias&#39;</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span>
                <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
                    <span class="n">field_id</span> <span class="o">=</span> <span class="n">field_id_prefix</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">counter</span><span class="p">)</span>
                    <span class="n">counter</span> <span class="o">+=</span> <span class="mi">1</span>
                    <span class="k">if</span> <span class="n">field_id</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">field_names</span><span class="p">:</span>
                        <span class="k">break</span>
                <span class="n">expressions</span><span class="p">[</span><span class="n">field_id</span><span class="p">]</span> <span class="o">=</span> <span class="n">field</span>
                <span class="n">_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">field_id</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">_fields</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">field</span><span class="p">)</span>

        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_values</span><span class="p">(</span><span class="o">*</span><span class="n">_fields</span><span class="p">,</span> <span class="o">**</span><span class="n">expressions</span><span class="p">)</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">_iterable_class</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">NamedValuesListIterable</span> <span class="k">if</span> <span class="n">named</span>
            <span class="k">else</span> <span class="n">FlatValuesListIterable</span> <span class="k">if</span> <span class="n">flat</span>
            <span class="k">else</span> <span class="n">ValuesListIterable</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">clone</span>

    <span class="k">def</span> <span class="nf">dates</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field_name</span><span class="p">,</span> <span class="n">kind</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="s1">&#39;ASC&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of date objects representing all available dates for</span>
<span class="sd">        the given field_name, scoped to &#39;kind&#39;.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="n">kind</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;week&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">),</span> \
            <span class="s2">&quot;&#39;kind&#39; must be one of &#39;year&#39;, &#39;month&#39;, &#39;week&#39;, or &#39;day&#39;.&quot;</span>
        <span class="k">assert</span> <span class="n">order</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;ASC&#39;</span><span class="p">,</span> <span class="s1">&#39;DESC&#39;</span><span class="p">),</span> \
            <span class="s2">&quot;&#39;order&#39; must be either &#39;ASC&#39; or &#39;DESC&#39;.&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
            <span class="n">datefield</span><span class="o">=</span><span class="n">Trunc</span><span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">kind</span><span class="p">,</span> <span class="n">output_field</span><span class="o">=</span><span class="n">DateField</span><span class="p">()),</span>
            <span class="n">plain_field</span><span class="o">=</span><span class="n">F</span><span class="p">(</span><span class="n">field_name</span><span class="p">)</span>
        <span class="p">)</span><span class="o">.</span><span class="n">values_list</span><span class="p">(</span>
            <span class="s1">&#39;datefield&#39;</span><span class="p">,</span> <span class="n">flat</span><span class="o">=</span><span class="kc">True</span>
        <span class="p">)</span><span class="o">.</span><span class="n">distinct</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">plain_field__isnull</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span><span class="o">.</span><span class="n">order_by</span><span class="p">((</span><span class="s1">&#39;-&#39;</span> <span class="k">if</span> <span class="n">order</span> <span class="o">==</span> <span class="s1">&#39;DESC&#39;</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;datefield&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">datetimes</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field_name</span><span class="p">,</span> <span class="n">kind</span><span class="p">,</span> <span class="n">order</span><span class="o">=</span><span class="s1">&#39;ASC&#39;</span><span class="p">,</span> <span class="n">tzinfo</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a list of datetime objects representing all available</span>
<span class="sd">        datetimes for the given field_name, scoped to &#39;kind&#39;.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="n">kind</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;year&#39;</span><span class="p">,</span> <span class="s1">&#39;month&#39;</span><span class="p">,</span> <span class="s1">&#39;week&#39;</span><span class="p">,</span> <span class="s1">&#39;day&#39;</span><span class="p">,</span> <span class="s1">&#39;hour&#39;</span><span class="p">,</span> <span class="s1">&#39;minute&#39;</span><span class="p">,</span> <span class="s1">&#39;second&#39;</span><span class="p">),</span> \
            <span class="s2">&quot;&#39;kind&#39; must be one of &#39;year&#39;, &#39;month&#39;, &#39;week&#39;, &#39;day&#39;, &#39;hour&#39;, &#39;minute&#39;, or &#39;second&#39;.&quot;</span>
        <span class="k">assert</span> <span class="n">order</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;ASC&#39;</span><span class="p">,</span> <span class="s1">&#39;DESC&#39;</span><span class="p">),</span> \
            <span class="s2">&quot;&#39;order&#39; must be either &#39;ASC&#39; or &#39;DESC&#39;.&quot;</span>
        <span class="k">if</span> <span class="n">settings</span><span class="o">.</span><span class="n">USE_TZ</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">tzinfo</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">tzinfo</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">get_current_timezone</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">tzinfo</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">annotate</span><span class="p">(</span>
            <span class="n">datetimefield</span><span class="o">=</span><span class="n">Trunc</span><span class="p">(</span><span class="n">field_name</span><span class="p">,</span> <span class="n">kind</span><span class="p">,</span> <span class="n">output_field</span><span class="o">=</span><span class="n">DateTimeField</span><span class="p">(),</span> <span class="n">tzinfo</span><span class="o">=</span><span class="n">tzinfo</span><span class="p">),</span>
            <span class="n">plain_field</span><span class="o">=</span><span class="n">F</span><span class="p">(</span><span class="n">field_name</span><span class="p">)</span>
        <span class="p">)</span><span class="o">.</span><span class="n">values_list</span><span class="p">(</span>
            <span class="s1">&#39;datetimefield&#39;</span><span class="p">,</span> <span class="n">flat</span><span class="o">=</span><span class="kc">True</span>
        <span class="p">)</span><span class="o">.</span><span class="n">distinct</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">plain_field__isnull</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span><span class="o">.</span><span class="n">order_by</span><span class="p">((</span><span class="s1">&#39;-&#39;</span> <span class="k">if</span> <span class="n">order</span> <span class="o">==</span> <span class="s1">&#39;DESC&#39;</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;datetimefield&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">none</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return an empty QuerySet.&quot;&quot;&quot;</span>
        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">set_empty</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">clone</span>

    <span class="c1">##################################################################</span>
    <span class="c1"># PUBLIC METHODS THAT ALTER ATTRIBUTES AND RETURN A NEW QUERYSET #</span>
    <span class="c1">##################################################################</span>

    <span class="k">def</span> <span class="nf">all</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a new QuerySet that is a copy of the current one. This allows a</span>
<span class="sd">        QuerySet to proxy for a model manager in some cases.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a new QuerySet instance with the args ANDed to the existing</span>
<span class="sd">        set.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_filter_or_exclude</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">exclude</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a new QuerySet instance with NOT (args) ANDed to the existing</span>
<span class="sd">        set.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_filter_or_exclude</span><span class="p">(</span><span class="kc">True</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_filter_or_exclude</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">negate</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">args</span> <span class="ow">or</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">can_filter</span><span class="p">(),</span> \
                <span class="s2">&quot;Cannot filter a query once a slice has been taken.&quot;</span>

        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">negate</span><span class="p">:</span>
            <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">add_q</span><span class="p">(</span><span class="o">~</span><span class="n">Q</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">add_q</span><span class="p">(</span><span class="n">Q</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">))</span>
        <span class="k">return</span> <span class="n">clone</span>

    <span class="k">def</span> <span class="nf">complex_filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">filter_obj</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a new QuerySet instance with filter_obj added to the filters.</span>

<span class="sd">        filter_obj can be a Q object or a dictionary of keyword lookup</span>
<span class="sd">        arguments.</span>

<span class="sd">        This exists to support framework features such as &#39;limit_choices_to&#39;,</span>
<span class="sd">        and usually it will be more natural to use other methods.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">filter_obj</span><span class="p">,</span> <span class="n">Q</span><span class="p">):</span>
            <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
            <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">add_q</span><span class="p">(</span><span class="n">filter_obj</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">clone</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_filter_or_exclude</span><span class="p">(</span><span class="kc">None</span><span class="p">,</span> <span class="o">**</span><span class="n">filter_obj</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_combinator_query</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">combinator</span><span class="p">,</span> <span class="o">*</span><span class="n">other_qs</span><span class="p">,</span> <span class="nb">all</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="c1"># Clone the query to inherit the select list and everything</span>
        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="c1"># Clear limits and ordering so they can be reapplied</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">clear_ordering</span><span class="p">(</span><span class="kc">True</span><span class="p">)</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">clear_limits</span><span class="p">()</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">combined_queries</span> <span class="o">=</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="p">,)</span> <span class="o">+</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">qs</span><span class="o">.</span><span class="n">query</span> <span class="k">for</span> <span class="n">qs</span> <span class="ow">in</span> <span class="n">other_qs</span><span class="p">)</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">combinator</span> <span class="o">=</span> <span class="n">combinator</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">combinator_all</span> <span class="o">=</span> <span class="nb">all</span>
        <span class="k">return</span> <span class="n">clone</span>

    <span class="k">def</span> <span class="nf">union</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">other_qs</span><span class="p">,</span> <span class="nb">all</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="c1"># If the query is an EmptyQuerySet, combine all nonempty querysets.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">EmptyQuerySet</span><span class="p">):</span>
            <span class="n">qs</span> <span class="o">=</span> <span class="p">[</span><span class="n">q</span> <span class="k">for</span> <span class="n">q</span> <span class="ow">in</span> <span class="n">other_qs</span> <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">EmptyQuerySet</span><span class="p">)]</span>
            <span class="k">return</span> <span class="n">qs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">_combinator_query</span><span class="p">(</span><span class="s1">&#39;union&#39;</span><span class="p">,</span> <span class="o">*</span><span class="n">qs</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="nb">all</span><span class="o">=</span><span class="nb">all</span><span class="p">)</span> <span class="k">if</span> <span class="n">qs</span> <span class="k">else</span> <span class="bp">self</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_combinator_query</span><span class="p">(</span><span class="s1">&#39;union&#39;</span><span class="p">,</span> <span class="o">*</span><span class="n">other_qs</span><span class="p">,</span> <span class="nb">all</span><span class="o">=</span><span class="nb">all</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">intersection</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">other_qs</span><span class="p">):</span>
        <span class="c1"># If any query is an EmptyQuerySet, return it.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">EmptyQuerySet</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="k">for</span> <span class="n">other</span> <span class="ow">in</span> <span class="n">other_qs</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">EmptyQuerySet</span><span class="p">):</span>
                <span class="k">return</span> <span class="n">other</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_combinator_query</span><span class="p">(</span><span class="s1">&#39;intersection&#39;</span><span class="p">,</span> <span class="o">*</span><span class="n">other_qs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">difference</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">other_qs</span><span class="p">):</span>
        <span class="c1"># If the query is an EmptyQuerySet, return it.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">EmptyQuerySet</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_combinator_query</span><span class="p">(</span><span class="s1">&#39;difference&#39;</span><span class="p">,</span> <span class="o">*</span><span class="n">other_qs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">select_for_update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">nowait</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">skip_locked</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">of</span><span class="o">=</span><span class="p">()):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a new QuerySet instance that will select objects with a</span>
<span class="sd">        FOR UPDATE lock.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">nowait</span> <span class="ow">and</span> <span class="n">skip_locked</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;The nowait option cannot be used with skip_locked.&#39;</span><span class="p">)</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">_for_write</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">select_for_update</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">select_for_update_nowait</span> <span class="o">=</span> <span class="n">nowait</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">select_for_update_skip_locked</span> <span class="o">=</span> <span class="n">skip_locked</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">select_for_update_of</span> <span class="o">=</span> <span class="n">of</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">select_related</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">fields</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a new QuerySet instance that will select related objects.</span>

<span class="sd">        If fields are specified, they must be ForeignKey fields and only those</span>
<span class="sd">        related objects are included in the selection.</span>

<span class="sd">        If select_related(None) is called, clear the list.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Cannot call select_related() after .values() or .values_list()&quot;</span><span class="p">)</span>

        <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">fields</span> <span class="o">==</span> <span class="p">(</span><span class="kc">None</span><span class="p">,):</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">select_related</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">elif</span> <span class="n">fields</span><span class="p">:</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">add_select_related</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">select_related</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">prefetch_related</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">lookups</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a new QuerySet instance that will prefetch the specified</span>
<span class="sd">        Many-To-One and Many-To-Many related objects when the QuerySet is</span>
<span class="sd">        evaluated.</span>

<span class="sd">        When prefetch_related() is called more than once, append to the list of</span>
<span class="sd">        prefetch lookups. If prefetch_related(None) is called, clear the list.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">lookups</span> <span class="o">==</span> <span class="p">(</span><span class="kc">None</span><span class="p">,):</span>
            <span class="n">clone</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="o">=</span> <span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">lookup</span> <span class="ow">in</span> <span class="n">lookups</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">lookup</span><span class="p">,</span> <span class="n">Prefetch</span><span class="p">):</span>
                    <span class="n">lookup</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">prefetch_to</span>
                <span class="n">lookup</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">LOOKUP_SEP</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">lookup</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">_filtered_relations</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;prefetch_related() is not supported with FilteredRelation.&#39;</span><span class="p">)</span>
            <span class="n">clone</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="o">=</span> <span class="n">clone</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="o">+</span> <span class="n">lookups</span>
        <span class="k">return</span> <span class="n">clone</span>

    <span class="k">def</span> <span class="nf">annotate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a query set in which the returned objects have been annotated</span>
<span class="sd">        with extra data or aggregations.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_validate_values_are_expressions</span><span class="p">(</span><span class="n">args</span> <span class="o">+</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">values</span><span class="p">()),</span> <span class="n">method_name</span><span class="o">=</span><span class="s1">&#39;annotate&#39;</span><span class="p">)</span>
        <span class="n">annotations</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>  <span class="c1"># To preserve ordering of args</span>
        <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span>
            <span class="c1"># The default_alias property may raise a TypeError.</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">arg</span><span class="o">.</span><span class="n">default_alias</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The named annotation &#39;</span><span class="si">%s</span><span class="s2">&#39; conflicts with the &quot;</span>
                                     <span class="s2">&quot;default name for another annotation.&quot;</span>
                                     <span class="o">%</span> <span class="n">arg</span><span class="o">.</span><span class="n">default_alias</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Complex annotations require an alias&quot;</span><span class="p">)</span>
            <span class="n">annotations</span><span class="p">[</span><span class="n">arg</span><span class="o">.</span><span class="n">default_alias</span><span class="p">]</span> <span class="o">=</span> <span class="n">arg</span>
        <span class="n">annotations</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="n">names</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fields</span>
        <span class="k">if</span> <span class="n">names</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">names</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">chain</span><span class="o">.</span><span class="n">from_iterable</span><span class="p">(</span>
                <span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">field</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span> <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="s1">&#39;attname&#39;</span><span class="p">)</span> <span class="k">else</span> <span class="p">(</span><span class="n">field</span><span class="o">.</span><span class="n">name</span><span class="p">,)</span>
                <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_fields</span><span class="p">()</span>
            <span class="p">))</span>

        <span class="k">for</span> <span class="n">alias</span><span class="p">,</span> <span class="n">annotation</span> <span class="ow">in</span> <span class="n">annotations</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">alias</span> <span class="ow">in</span> <span class="n">names</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The annotation &#39;</span><span class="si">%s</span><span class="s2">&#39; conflicts with a field on &quot;</span>
                                 <span class="s2">&quot;the model.&quot;</span> <span class="o">%</span> <span class="n">alias</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">annotation</span><span class="p">,</span> <span class="n">FilteredRelation</span><span class="p">):</span>
                <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">add_filtered_relation</span><span class="p">(</span><span class="n">annotation</span><span class="p">,</span> <span class="n">alias</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">add_annotation</span><span class="p">(</span><span class="n">annotation</span><span class="p">,</span> <span class="n">alias</span><span class="p">,</span> <span class="n">is_summary</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">alias</span><span class="p">,</span> <span class="n">annotation</span> <span class="ow">in</span> <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">annotations</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">alias</span> <span class="ow">in</span> <span class="n">annotations</span> <span class="ow">and</span> <span class="n">annotation</span><span class="o">.</span><span class="n">contains_aggregate</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">clone</span><span class="o">.</span><span class="n">_fields</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">group_by</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">set_group_by</span><span class="p">()</span>
                <span class="k">break</span>

        <span class="k">return</span> <span class="n">clone</span>

    <span class="k">def</span> <span class="nf">order_by</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">field_names</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a new QuerySet instance with the ordering changed.&quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">can_filter</span><span class="p">(),</span> \
            <span class="s2">&quot;Cannot reorder a query once a slice has been taken.&quot;</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">clear_ordering</span><span class="p">(</span><span class="n">force_empty</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">add_ordering</span><span class="p">(</span><span class="o">*</span><span class="n">field_names</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">distinct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">field_names</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a new QuerySet instance that will select only distinct results.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">can_filter</span><span class="p">(),</span> \
            <span class="s2">&quot;Cannot create distinct fields once a slice has been taken.&quot;</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">add_distinct_fields</span><span class="p">(</span><span class="o">*</span><span class="n">field_names</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">extra</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">select</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">where</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">tables</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
              <span class="n">order_by</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">select_params</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Add extra SQL fragments to the query.&quot;&quot;&quot;</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">can_filter</span><span class="p">(),</span> \
            <span class="s2">&quot;Cannot change a query once a slice has been taken&quot;</span>
        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">add_extra</span><span class="p">(</span><span class="n">select</span><span class="p">,</span> <span class="n">select_params</span><span class="p">,</span> <span class="n">where</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">tables</span><span class="p">,</span> <span class="n">order_by</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">clone</span>

    <span class="k">def</span> <span class="nf">reverse</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Reverse the ordering of the QuerySet.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">can_filter</span><span class="p">():</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;Cannot reverse a query once a slice has been taken.&#39;</span><span class="p">)</span>
        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">standard_ordering</span> <span class="o">=</span> <span class="ow">not</span> <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">standard_ordering</span>
        <span class="k">return</span> <span class="n">clone</span>

    <span class="k">def</span> <span class="nf">defer</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">fields</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Defer the loading of data for certain fields until they are accessed.</span>
<span class="sd">        Add the set of deferred fields to any existing set of deferred fields.</span>
<span class="sd">        The only exception to this is if None is passed in as the only</span>
<span class="sd">        parameter, in which case removal all deferrals.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Cannot call defer() after .values() or .values_list()&quot;</span><span class="p">)</span>
        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">fields</span> <span class="o">==</span> <span class="p">(</span><span class="kc">None</span><span class="p">,):</span>
            <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">clear_deferred_loading</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">add_deferred_loading</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">clone</span>

    <span class="k">def</span> <span class="nf">only</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">fields</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Essentially, the opposite of defer(). Only the fields passed into this</span>
<span class="sd">        method and that are not already specified as deferred are loaded</span>
<span class="sd">        immediately when the queryset is evaluated.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Cannot call only() after .values() or .values_list()&quot;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">fields</span> <span class="o">==</span> <span class="p">(</span><span class="kc">None</span><span class="p">,):</span>
            <span class="c1"># Can only pass None to defer(), not only(), as the rest option.</span>
            <span class="c1"># That won&#39;t stop people trying to do this, so let&#39;s be explicit.</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Cannot pass None as an argument to only().&quot;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="n">fields</span><span class="p">:</span>
            <span class="n">field</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">LOOKUP_SEP</span><span class="p">,</span> <span class="mi">1</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">_filtered_relations</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;only() is not supported with FilteredRelation.&#39;</span><span class="p">)</span>
        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">add_immediate_loading</span><span class="p">(</span><span class="n">fields</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">clone</span>

    <span class="k">def</span> <span class="nf">using</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">alias</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Select which database this QuerySet should execute against.&quot;&quot;&quot;</span>
        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_chain</span><span class="p">()</span>
        <span class="n">clone</span><span class="o">.</span><span class="n">_db</span> <span class="o">=</span> <span class="n">alias</span>
        <span class="k">return</span> <span class="n">clone</span>

    <span class="c1">###################################</span>
    <span class="c1"># PUBLIC INTROSPECTION ATTRIBUTES #</span>
    <span class="c1">###################################</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">ordered</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return True if the QuerySet is ordered -- i.e. has an order_by()</span>
<span class="sd">        clause or a default ordering on the model (or is empty).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">EmptyQuerySet</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">extra_order_by</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">order_by</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">default_ordering</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">get_meta</span><span class="p">()</span><span class="o">.</span><span class="n">ordering</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">db</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the database used if this query is executed now.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_for_write</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_db</span> <span class="ow">or</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_write</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">_hints</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_db</span> <span class="ow">or</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_read</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">_hints</span><span class="p">)</span>

    <span class="c1">###################</span>
    <span class="c1"># PRIVATE METHODS #</span>
    <span class="c1">###################</span>

    <span class="k">def</span> <span class="nf">_insert</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">objs</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">return_id</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">raw</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">ignore_conflicts</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Insert a new record for the given model. This provides an interface to</span>
<span class="sd">        the InsertQuery class and is how Model.save() is implemented.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_for_write</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="n">using</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">using</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span>
        <span class="n">query</span> <span class="o">=</span> <span class="n">sql</span><span class="o">.</span><span class="n">InsertQuery</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">ignore_conflicts</span><span class="o">=</span><span class="n">ignore_conflicts</span><span class="p">)</span>
        <span class="n">query</span><span class="o">.</span><span class="n">insert_values</span><span class="p">(</span><span class="n">fields</span><span class="p">,</span> <span class="n">objs</span><span class="p">,</span> <span class="n">raw</span><span class="o">=</span><span class="n">raw</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">query</span><span class="o">.</span><span class="n">get_compiler</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">using</span><span class="p">)</span><span class="o">.</span><span class="n">execute_sql</span><span class="p">(</span><span class="n">return_id</span><span class="p">)</span>
    <span class="n">_insert</span><span class="o">.</span><span class="n">alters_data</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="n">_insert</span><span class="o">.</span><span class="n">queryset_only</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">_batched_insert</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">objs</span><span class="p">,</span> <span class="n">fields</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">,</span> <span class="n">ignore_conflicts</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Helper method for bulk_create() to insert objs one batch at a time.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">ignore_conflicts</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">connections</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">]</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">supports_ignore_conflicts</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">NotSupportedError</span><span class="p">(</span><span class="s1">&#39;This database backend does not support ignoring conflicts.&#39;</span><span class="p">)</span>
        <span class="n">ops</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">]</span><span class="o">.</span><span class="n">ops</span>
        <span class="n">batch_size</span> <span class="o">=</span> <span class="p">(</span><span class="n">batch_size</span> <span class="ow">or</span> <span class="nb">max</span><span class="p">(</span><span class="n">ops</span><span class="o">.</span><span class="n">bulk_batch_size</span><span class="p">(</span><span class="n">fields</span><span class="p">,</span> <span class="n">objs</span><span class="p">),</span> <span class="mi">1</span><span class="p">))</span>
        <span class="n">inserted_ids</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">bulk_return</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">]</span><span class="o">.</span><span class="n">features</span><span class="o">.</span><span class="n">can_return_ids_from_bulk_insert</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="p">[</span><span class="n">objs</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">i</span> <span class="o">+</span> <span class="n">batch_size</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">objs</span><span class="p">),</span> <span class="n">batch_size</span><span class="p">)]:</span>
            <span class="k">if</span> <span class="n">bulk_return</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">ignore_conflicts</span><span class="p">:</span>
                <span class="n">inserted_id</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_insert</span><span class="p">(</span>
                    <span class="n">item</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="n">fields</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="n">return_id</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                    <span class="n">ignore_conflicts</span><span class="o">=</span><span class="n">ignore_conflicts</span><span class="p">,</span>
                <span class="p">)</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">inserted_id</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
                    <span class="n">inserted_ids</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">inserted_id</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">inserted_ids</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">inserted_id</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_insert</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="n">fields</span><span class="o">=</span><span class="n">fields</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="n">ignore_conflicts</span><span class="o">=</span><span class="n">ignore_conflicts</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">inserted_ids</span>

    <span class="k">def</span> <span class="nf">_chain</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a copy of the current QuerySet that&#39;s ready for another</span>
<span class="sd">        operation.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_clone</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">obj</span><span class="o">.</span><span class="n">_sticky_filter</span><span class="p">:</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">filter_is_sticky</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">obj</span><span class="o">.</span><span class="n">_sticky_filter</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="n">obj</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">_clone</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a copy of the current QuerySet. A lightweight alternative</span>
<span class="sd">        to deepcopy().</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">c</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">query</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">chain</span><span class="p">(),</span> <span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="p">,</span> <span class="n">hints</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_hints</span><span class="p">)</span>
        <span class="n">c</span><span class="o">.</span><span class="n">_sticky_filter</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sticky_filter</span>
        <span class="n">c</span><span class="o">.</span><span class="n">_for_write</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_for_write</span>
        <span class="n">c</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span><span class="p">[:]</span>
        <span class="n">c</span><span class="o">.</span><span class="n">_known_related_objects</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_known_related_objects</span>
        <span class="n">c</span><span class="o">.</span><span class="n">_iterable_class</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_iterable_class</span>
        <span class="n">c</span><span class="o">.</span><span class="n">_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fields</span>
        <span class="k">return</span> <span class="n">c</span>

    <span class="k">def</span> <span class="nf">_fetch_all</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_iterable_class</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_done</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_related_objects</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_next_is_sticky</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Indicate that the next filter call and the one following that should</span>
<span class="sd">        be treated as a single filter. This is only important when it comes to</span>
<span class="sd">        determining when to reuse tables for many-to-many filters. Required so</span>
<span class="sd">        that we can filter naturally on the results of related managers.</span>

<span class="sd">        This doesn&#39;t return a clone of the current QuerySet (it returns</span>
<span class="sd">        &quot;self&quot;). The method is only used internally and should be immediately</span>
<span class="sd">        followed by a filter() that does create a clone.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_sticky_filter</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">_merge_sanity_check</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Check that two QuerySet classes may be merged.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fields</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="p">(</span>
                <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">values_select</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">set</span><span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">values_select</span><span class="p">)</span> <span class="ow">or</span>
                <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">extra_select</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">set</span><span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">extra_select</span><span class="p">)</span> <span class="ow">or</span>
                <span class="nb">set</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">annotation_select</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">set</span><span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">annotation_select</span><span class="p">)):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
                <span class="s2">&quot;Merging &#39;</span><span class="si">%s</span><span class="s2">&#39; classes must involve the same values in each case.&quot;</span>
                <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">_merge_known_related_objects</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Keep track of all known related objects from either QuerySet instance.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">for</span> <span class="n">field</span><span class="p">,</span> <span class="n">objects</span> <span class="ow">in</span> <span class="n">other</span><span class="o">.</span><span class="n">_known_related_objects</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_known_related_objects</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="p">{})</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">objects</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">resolve_expression</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fields</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fields</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="c1"># values() queryset can only be used as nested queries</span>
            <span class="c1"># if they are set up to select only a single field.</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;Cannot use multi-field values as a filter value.&#39;</span><span class="p">)</span>
        <span class="n">query</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">resolve_expression</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">query</span><span class="o">.</span><span class="n">_db</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_db</span>
        <span class="k">return</span> <span class="n">query</span>
    <span class="n">resolve_expression</span><span class="o">.</span><span class="n">queryset_only</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_add_hints</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">hints</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Update hinting information for use by routers. Add new key/values or</span>
<span class="sd">        overwrite existing key/values.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_hints</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">hints</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_has_filters</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Check if this QuerySet has any filtering going on. This isn&#39;t</span>
<span class="sd">        equivalent with checking if all objects are present in results, for</span>
<span class="sd">        example, qs[1:]._has_filters() -&gt; False.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">has_filters</span><span class="p">()</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_validate_values_are_expressions</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">method_name</span><span class="p">):</span>
        <span class="n">invalid_args</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span> <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">values</span> <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="s1">&#39;resolve_expression&#39;</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">invalid_args</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span>
                <span class="s1">&#39;QuerySet.</span><span class="si">%s</span><span class="s1">() received non-expression(s): </span><span class="si">%s</span><span class="s1">.&#39;</span> <span class="o">%</span> <span class="p">(</span>
                    <span class="n">method_name</span><span class="p">,</span>
                    <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">invalid_args</span><span class="p">),</span>
                <span class="p">)</span>
            <span class="p">)</span></div>


<span class="k">class</span> <span class="nc">InstanceCheckMeta</span><span class="p">(</span><span class="nb">type</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__instancecheck__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">instance</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">QuerySet</span><span class="p">)</span> <span class="ow">and</span> <span class="n">instance</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">is_empty</span><span class="p">()</span>


<span class="k">class</span> <span class="nc">EmptyQuerySet</span><span class="p">(</span><span class="n">metaclass</span><span class="o">=</span><span class="n">InstanceCheckMeta</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Marker class to checking if a queryset is empty by .none():</span>
<span class="sd">        isinstance(qs.none(), EmptyQuerySet) -&gt; True</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;EmptyQuerySet can&#39;t be instantiated&quot;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">RawQuerySet</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Provide an iterator which converts the results of raw SQL queries into</span>
<span class="sd">    annotated model instances.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">raw_query</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">query</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">translations</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">hints</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">raw_query</span> <span class="o">=</span> <span class="n">raw_query</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_db</span> <span class="o">=</span> <span class="n">using</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_hints</span> <span class="o">=</span> <span class="n">hints</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">query</span> <span class="o">=</span> <span class="n">query</span> <span class="ow">or</span> <span class="n">sql</span><span class="o">.</span><span class="n">RawQuery</span><span class="p">(</span><span class="n">sql</span><span class="o">=</span><span class="n">raw_query</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">params</span> <span class="o">=</span> <span class="n">params</span> <span class="ow">or</span> <span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">translations</span> <span class="o">=</span> <span class="n">translations</span> <span class="ow">or</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="o">=</span> <span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_done</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">resolve_model_init_order</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Resolve the init field names and value positions.&quot;&quot;&quot;</span>
        <span class="n">converter</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">]</span><span class="o">.</span><span class="n">introspection</span><span class="o">.</span><span class="n">identifier_converter</span>
        <span class="n">model_init_fields</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span> <span class="k">if</span> <span class="n">converter</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">column</span><span class="p">)</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">columns</span><span class="p">]</span>
        <span class="n">annotation_fields</span> <span class="o">=</span> <span class="p">[(</span><span class="n">column</span><span class="p">,</span> <span class="n">pos</span><span class="p">)</span> <span class="k">for</span> <span class="n">pos</span><span class="p">,</span> <span class="n">column</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">columns</span><span class="p">)</span>
                             <span class="k">if</span> <span class="n">column</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_fields</span><span class="p">]</span>
        <span class="n">model_init_order</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">columns</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">converter</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">column</span><span class="p">))</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">model_init_fields</span><span class="p">]</span>
        <span class="n">model_init_names</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span><span class="o">.</span><span class="n">attname</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">model_init_fields</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">model_init_names</span><span class="p">,</span> <span class="n">model_init_order</span><span class="p">,</span> <span class="n">annotation_fields</span>

    <span class="k">def</span> <span class="nf">prefetch_related</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">lookups</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Same as QuerySet.prefetch_related()&quot;&quot;&quot;</span>
        <span class="n">clone</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_clone</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">lookups</span> <span class="o">==</span> <span class="p">(</span><span class="kc">None</span><span class="p">,):</span>
            <span class="n">clone</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="o">=</span> <span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">clone</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="o">=</span> <span class="n">clone</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="o">+</span> <span class="n">lookups</span>
        <span class="k">return</span> <span class="n">clone</span>

    <span class="k">def</span> <span class="nf">_prefetch_related_objects</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">prefetch_related_objects</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span><span class="p">,</span> <span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_done</span> <span class="o">=</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">_clone</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Same as QuerySet._clone()&quot;&quot;&quot;</span>
        <span class="n">c</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">raw_query</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">query</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">,</span>
            <span class="n">translations</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">translations</span><span class="p">,</span> <span class="n">using</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_db</span><span class="p">,</span> <span class="n">hints</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_hints</span>
        <span class="p">)</span>
        <span class="n">c</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span><span class="p">[:]</span>
        <span class="k">return</span> <span class="n">c</span>

    <span class="k">def</span> <span class="nf">_fetch_all</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">iterator</span><span class="p">())</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_done</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_prefetch_related_objects</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fetch_all</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__bool__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fetch_all</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fetch_all</span><span class="p">()</span>
        <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_result_cache</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">iterator</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># Cache some things for performance reasons outside the loop.</span>
        <span class="n">db</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span>
        <span class="n">compiler</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="n">db</span><span class="p">]</span><span class="o">.</span><span class="n">ops</span><span class="o">.</span><span class="n">compiler</span><span class="p">(</span><span class="s1">&#39;SQLCompiler&#39;</span><span class="p">)(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="p">,</span> <span class="n">connections</span><span class="p">[</span><span class="n">db</span><span class="p">],</span> <span class="n">db</span>
        <span class="p">)</span>

        <span class="n">query</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">model_init_names</span><span class="p">,</span> <span class="n">model_init_pos</span><span class="p">,</span> <span class="n">annotation_fields</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">resolve_model_init_order</span><span class="p">()</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">attname</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">model_init_names</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">InvalidQuery</span><span class="p">(</span><span class="s1">&#39;Raw query must include the primary key&#39;</span><span class="p">)</span>
            <span class="n">model_cls</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span>
            <span class="n">fields</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">model_fields</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">columns</span><span class="p">]</span>
            <span class="n">converters</span> <span class="o">=</span> <span class="n">compiler</span><span class="o">.</span><span class="n">get_converters</span><span class="p">([</span>
                <span class="n">f</span><span class="o">.</span><span class="n">get_col</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">db_table</span><span class="p">)</span> <span class="k">if</span> <span class="n">f</span> <span class="k">else</span> <span class="kc">None</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">fields</span>
            <span class="p">])</span>
            <span class="k">if</span> <span class="n">converters</span><span class="p">:</span>
                <span class="n">query</span> <span class="o">=</span> <span class="n">compiler</span><span class="o">.</span><span class="n">apply_converters</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">converters</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">values</span> <span class="ow">in</span> <span class="n">query</span><span class="p">:</span>
                <span class="c1"># Associate fields to values</span>
                <span class="n">model_init_values</span> <span class="o">=</span> <span class="p">[</span><span class="n">values</span><span class="p">[</span><span class="n">pos</span><span class="p">]</span> <span class="k">for</span> <span class="n">pos</span> <span class="ow">in</span> <span class="n">model_init_pos</span><span class="p">]</span>
                <span class="n">instance</span> <span class="o">=</span> <span class="n">model_cls</span><span class="o">.</span><span class="n">from_db</span><span class="p">(</span><span class="n">db</span><span class="p">,</span> <span class="n">model_init_names</span><span class="p">,</span> <span class="n">model_init_values</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">annotation_fields</span><span class="p">:</span>
                    <span class="k">for</span> <span class="n">column</span><span class="p">,</span> <span class="n">pos</span> <span class="ow">in</span> <span class="n">annotation_fields</span><span class="p">:</span>
                        <span class="nb">setattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">column</span><span class="p">,</span> <span class="n">values</span><span class="p">[</span><span class="n">pos</span><span class="p">])</span>
                <span class="k">yield</span> <span class="n">instance</span>
        <span class="k">finally</span><span class="p">:</span>
            <span class="c1"># Done iterating the Query. If it has its own cursor, close it.</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="p">,</span> <span class="s1">&#39;cursor&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">cursor</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">cursor</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s2">&quot;&lt;</span><span class="si">%s</span><span class="s2">: </span><span class="si">%s</span><span class="s2">&gt;&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="p">)[</span><span class="n">k</span><span class="p">]</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">db</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return the database used if this query is executed now.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_db</span> <span class="ow">or</span> <span class="n">router</span><span class="o">.</span><span class="n">db_for_read</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="o">**</span><span class="bp">self</span><span class="o">.</span><span class="n">_hints</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">using</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">alias</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Select the database this RawQuerySet should execute against.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">RawQuerySet</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">raw_query</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span>
            <span class="n">query</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">chain</span><span class="p">(</span><span class="n">using</span><span class="o">=</span><span class="n">alias</span><span class="p">),</span>
            <span class="n">params</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">params</span><span class="p">,</span> <span class="n">translations</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">translations</span><span class="p">,</span>
            <span class="n">using</span><span class="o">=</span><span class="n">alias</span><span class="p">,</span>
        <span class="p">)</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">columns</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        A list of model field names in the order they&#39;ll appear in the</span>
<span class="sd">        query results.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">columns</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">query</span><span class="o">.</span><span class="n">get_columns</span><span class="p">()</span>
        <span class="c1"># Adjust any column names which don&#39;t match field names</span>
        <span class="k">for</span> <span class="p">(</span><span class="n">query_name</span><span class="p">,</span> <span class="n">model_name</span><span class="p">)</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">translations</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="c1"># Ignore translations for nonexistent column names</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">index</span> <span class="o">=</span> <span class="n">columns</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">query_name</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">columns</span><span class="p">[</span><span class="n">index</span><span class="p">]</span> <span class="o">=</span> <span class="n">model_name</span>
        <span class="k">return</span> <span class="n">columns</span>

    <span class="nd">@cached_property</span>
    <span class="k">def</span> <span class="nf">model_fields</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;A dict mapping column names to model field names.&quot;&quot;&quot;</span>
        <span class="n">converter</span> <span class="o">=</span> <span class="n">connections</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">]</span><span class="o">.</span><span class="n">introspection</span><span class="o">.</span><span class="n">identifier_converter</span>
        <span class="n">model_fields</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">field</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">fields</span><span class="p">:</span>
            <span class="n">name</span><span class="p">,</span> <span class="n">column</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">get_attname_column</span><span class="p">()</span>
            <span class="n">model_fields</span><span class="p">[</span><span class="n">converter</span><span class="p">(</span><span class="n">column</span><span class="p">)]</span> <span class="o">=</span> <span class="n">field</span>
        <span class="k">return</span> <span class="n">model_fields</span>


<div class="viewcode-block" id="Prefetch"><a class="viewcode-back" href="../../../../ref/models/querysets.html#django.db.models.query.Prefetch">[docs]</a><span class="k">class</span> <span class="nc">Prefetch</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">lookup</span><span class="p">,</span> <span class="n">queryset</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">to_attr</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="c1"># `prefetch_through` is the path we traverse to perform the prefetch.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">prefetch_through</span> <span class="o">=</span> <span class="n">lookup</span>
        <span class="c1"># `prefetch_to` is the path to the attribute that stores the result.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">prefetch_to</span> <span class="o">=</span> <span class="n">lookup</span>
        <span class="k">if</span> <span class="n">queryset</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">queryset</span><span class="o">.</span><span class="n">_iterable_class</span><span class="p">,</span> <span class="n">ModelIterable</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Prefetch querysets cannot use values().&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">to_attr</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">prefetch_to</span> <span class="o">=</span> <span class="n">LOOKUP_SEP</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">lookup</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">LOOKUP_SEP</span><span class="p">)[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="n">to_attr</span><span class="p">])</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span> <span class="o">=</span> <span class="n">queryset</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">to_attr</span> <span class="o">=</span> <span class="n">to_attr</span>

    <span class="k">def</span> <span class="nf">__getstate__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">obj_dict</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="c1"># Prevent the QuerySet from being evaluated</span>
            <span class="n">obj_dict</span><span class="p">[</span><span class="s1">&#39;queryset&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span><span class="o">.</span><span class="n">_chain</span><span class="p">(</span>
                <span class="n">_result_cache</span><span class="o">=</span><span class="p">[],</span>
                <span class="n">_prefetch_done</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
            <span class="p">)</span>
        <span class="k">return</span> <span class="n">obj_dict</span>

    <span class="k">def</span> <span class="nf">add_prefix</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">prefix</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">prefetch_through</span> <span class="o">=</span> <span class="n">prefix</span> <span class="o">+</span> <span class="n">LOOKUP_SEP</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">prefetch_through</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">prefetch_to</span> <span class="o">=</span> <span class="n">prefix</span> <span class="o">+</span> <span class="n">LOOKUP_SEP</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">prefetch_to</span>

    <span class="k">def</span> <span class="nf">get_current_prefetch_to</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">level</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">LOOKUP_SEP</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">prefetch_to</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">LOOKUP_SEP</span><span class="p">)[:</span><span class="n">level</span> <span class="o">+</span> <span class="mi">1</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">get_current_to_attr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">level</span><span class="p">):</span>
        <span class="n">parts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">prefetch_to</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">LOOKUP_SEP</span><span class="p">)</span>
        <span class="n">to_attr</span> <span class="o">=</span> <span class="n">parts</span><span class="p">[</span><span class="n">level</span><span class="p">]</span>
        <span class="n">as_attr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">to_attr</span> <span class="ow">and</span> <span class="n">level</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">parts</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="n">to_attr</span><span class="p">,</span> <span class="n">as_attr</span>

    <span class="k">def</span> <span class="nf">get_current_queryset</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">level</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_current_prefetch_to</span><span class="p">(</span><span class="n">level</span><span class="p">)</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">prefetch_to</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">queryset</span>
        <span class="k">return</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">Prefetch</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">prefetch_to</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">prefetch_to</span>

    <span class="k">def</span> <span class="nf">__hash__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">hash</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">prefetch_to</span><span class="p">))</span></div>


<span class="k">def</span> <span class="nf">normalize_prefetch_lookups</span><span class="p">(</span><span class="n">lookups</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Normalize lookups into Prefetch objects.&quot;&quot;&quot;</span>
    <span class="n">ret</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">lookup</span> <span class="ow">in</span> <span class="n">lookups</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">lookup</span><span class="p">,</span> <span class="n">Prefetch</span><span class="p">):</span>
            <span class="n">lookup</span> <span class="o">=</span> <span class="n">Prefetch</span><span class="p">(</span><span class="n">lookup</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">prefix</span><span class="p">:</span>
            <span class="n">lookup</span><span class="o">.</span><span class="n">add_prefix</span><span class="p">(</span><span class="n">prefix</span><span class="p">)</span>
        <span class="n">ret</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">lookup</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">ret</span>


<div class="viewcode-block" id="prefetch_related_objects"><a class="viewcode-back" href="../../../../ref/models/querysets.html#django.db.models.query.prefetch_related_objects">[docs]</a><span class="k">def</span> <span class="nf">prefetch_related_objects</span><span class="p">(</span><span class="n">model_instances</span><span class="p">,</span> <span class="o">*</span><span class="n">related_lookups</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Populate prefetched object caches for a list of model instances based on</span>
<span class="sd">    the lookups/Prefetch instances given.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">model_instances</span><span class="p">:</span>
        <span class="k">return</span>  <span class="c1"># nothing to do</span>

    <span class="c1"># We need to be able to dynamically add to the list of prefetch_related</span>
    <span class="c1"># lookups that we look up (see below).  So we need some book keeping to</span>
    <span class="c1"># ensure we don&#39;t do duplicate work.</span>
    <span class="n">done_queries</span> <span class="o">=</span> <span class="p">{}</span>    <span class="c1"># dictionary of things like &#39;foo__bar&#39;: [results]</span>

    <span class="n">auto_lookups</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>  <span class="c1"># we add to this as we go through.</span>
    <span class="n">followed_descriptors</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>  <span class="c1"># recursion protection</span>

    <span class="n">all_lookups</span> <span class="o">=</span> <span class="n">normalize_prefetch_lookups</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">related_lookups</span><span class="p">))</span>
    <span class="k">while</span> <span class="n">all_lookups</span><span class="p">:</span>
        <span class="n">lookup</span> <span class="o">=</span> <span class="n">all_lookups</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">lookup</span><span class="o">.</span><span class="n">prefetch_to</span> <span class="ow">in</span> <span class="n">done_queries</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">lookup</span><span class="o">.</span><span class="n">queryset</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; lookup was already seen with a different queryset. &quot;</span>
                                 <span class="s2">&quot;You may need to adjust the ordering of your lookups.&quot;</span> <span class="o">%</span> <span class="n">lookup</span><span class="o">.</span><span class="n">prefetch_to</span><span class="p">)</span>

            <span class="k">continue</span>

        <span class="c1"># Top level, the list of objects to decorate is the result cache</span>
        <span class="c1"># from the primary QuerySet. It won&#39;t be for deeper levels.</span>
        <span class="n">obj_list</span> <span class="o">=</span> <span class="n">model_instances</span>

        <span class="n">through_attrs</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">prefetch_through</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">LOOKUP_SEP</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">level</span><span class="p">,</span> <span class="n">through_attr</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">through_attrs</span><span class="p">):</span>
            <span class="c1"># Prepare main instances</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">obj_list</span><span class="p">:</span>
                <span class="k">break</span>

            <span class="n">prefetch_to</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">get_current_prefetch_to</span><span class="p">(</span><span class="n">level</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">prefetch_to</span> <span class="ow">in</span> <span class="n">done_queries</span><span class="p">:</span>
                <span class="c1"># Skip any prefetching, and any object preparation</span>
                <span class="n">obj_list</span> <span class="o">=</span> <span class="n">done_queries</span><span class="p">[</span><span class="n">prefetch_to</span><span class="p">]</span>
                <span class="k">continue</span>

            <span class="c1"># Prepare objects:</span>
            <span class="n">good_objects</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">obj_list</span><span class="p">:</span>
                <span class="c1"># Since prefetching can re-use instances, it is possible to have</span>
                <span class="c1"># the same instance multiple times in obj_list, so obj might</span>
                <span class="c1"># already be prepared.</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s1">&#39;_prefetched_objects_cache&#39;</span><span class="p">):</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">obj</span><span class="o">.</span><span class="n">_prefetched_objects_cache</span> <span class="o">=</span> <span class="p">{}</span>
                    <span class="k">except</span> <span class="p">(</span><span class="ne">AttributeError</span><span class="p">,</span> <span class="ne">TypeError</span><span class="p">):</span>
                        <span class="c1"># Must be an immutable object from</span>
                        <span class="c1"># values_list(flat=True), for example (TypeError) or</span>
                        <span class="c1"># a QuerySet subclass that isn&#39;t returning Model</span>
                        <span class="c1"># instances (AttributeError), either in Django or a 3rd</span>
                        <span class="c1"># party. prefetch_related() doesn&#39;t make sense, so quit.</span>
                        <span class="n">good_objects</span> <span class="o">=</span> <span class="kc">False</span>
                        <span class="k">break</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">good_objects</span><span class="p">:</span>
                <span class="k">break</span>

            <span class="c1"># Descend down tree</span>

            <span class="c1"># We assume that objects retrieved are homogeneous (which is the premise</span>
            <span class="c1"># of prefetch_related), so what applies to first object applies to all.</span>
            <span class="n">first_obj</span> <span class="o">=</span> <span class="n">obj_list</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">to_attr</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">get_current_to_attr</span><span class="p">(</span><span class="n">level</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
            <span class="n">prefetcher</span><span class="p">,</span> <span class="n">descriptor</span><span class="p">,</span> <span class="n">attr_found</span><span class="p">,</span> <span class="n">is_fetched</span> <span class="o">=</span> <span class="n">get_prefetcher</span><span class="p">(</span><span class="n">first_obj</span><span class="p">,</span> <span class="n">through_attr</span><span class="p">,</span> <span class="n">to_attr</span><span class="p">)</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="n">attr_found</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="s2">&quot;Cannot find &#39;</span><span class="si">%s</span><span class="s2">&#39; on </span><span class="si">%s</span><span class="s2"> object, &#39;</span><span class="si">%s</span><span class="s2">&#39; is an invalid &quot;</span>
                                     <span class="s2">&quot;parameter to prefetch_related()&quot;</span> <span class="o">%</span>
                                     <span class="p">(</span><span class="n">through_attr</span><span class="p">,</span> <span class="n">first_obj</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">lookup</span><span class="o">.</span><span class="n">prefetch_through</span><span class="p">))</span>

            <span class="k">if</span> <span class="n">level</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">through_attrs</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">prefetcher</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="c1"># Last one, this *must* resolve to something that supports</span>
                <span class="c1"># prefetching, otherwise there is no point adding it and the</span>
                <span class="c1"># developer asking for it has made a mistake.</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; does not resolve to an item that supports &quot;</span>
                                 <span class="s2">&quot;prefetching - this is an invalid parameter to &quot;</span>
                                 <span class="s2">&quot;prefetch_related().&quot;</span> <span class="o">%</span> <span class="n">lookup</span><span class="o">.</span><span class="n">prefetch_through</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">prefetcher</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">is_fetched</span><span class="p">:</span>
                <span class="n">obj_list</span><span class="p">,</span> <span class="n">additional_lookups</span> <span class="o">=</span> <span class="n">prefetch_one_level</span><span class="p">(</span><span class="n">obj_list</span><span class="p">,</span> <span class="n">prefetcher</span><span class="p">,</span> <span class="n">lookup</span><span class="p">,</span> <span class="n">level</span><span class="p">)</span>
                <span class="c1"># We need to ensure we don&#39;t keep adding lookups from the</span>
                <span class="c1"># same relationships to stop infinite recursion. So, if we</span>
                <span class="c1"># are already on an automatically added lookup, don&#39;t add</span>
                <span class="c1"># the new lookups from relationships we&#39;ve seen already.</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">prefetch_to</span> <span class="ow">in</span> <span class="n">done_queries</span> <span class="ow">and</span> <span class="n">lookup</span> <span class="ow">in</span> <span class="n">auto_lookups</span> <span class="ow">and</span> <span class="n">descriptor</span> <span class="ow">in</span> <span class="n">followed_descriptors</span><span class="p">):</span>
                    <span class="n">done_queries</span><span class="p">[</span><span class="n">prefetch_to</span><span class="p">]</span> <span class="o">=</span> <span class="n">obj_list</span>
                    <span class="n">new_lookups</span> <span class="o">=</span> <span class="n">normalize_prefetch_lookups</span><span class="p">(</span><span class="nb">reversed</span><span class="p">(</span><span class="n">additional_lookups</span><span class="p">),</span> <span class="n">prefetch_to</span><span class="p">)</span>
                    <span class="n">auto_lookups</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">new_lookups</span><span class="p">)</span>
                    <span class="n">all_lookups</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">new_lookups</span><span class="p">)</span>
                <span class="n">followed_descriptors</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">descriptor</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Either a singly related object that has already been fetched</span>
                <span class="c1"># (e.g. via select_related), or hopefully some other property</span>
                <span class="c1"># that doesn&#39;t support prefetching but needs to be traversed.</span>

                <span class="c1"># We replace the current list of parent objects with the list</span>
                <span class="c1"># of related objects, filtering out empty or missing values so</span>
                <span class="c1"># that we can continue with nullable or reverse relations.</span>
                <span class="n">new_obj_list</span> <span class="o">=</span> <span class="p">[]</span>
                <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">obj_list</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">through_attr</span> <span class="ow">in</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="s1">&#39;_prefetched_objects_cache&#39;</span><span class="p">,</span> <span class="p">()):</span>
                        <span class="c1"># If related objects have been prefetched, use the</span>
                        <span class="c1"># cache rather than the object&#39;s through_attr.</span>
                        <span class="n">new_obj</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">obj</span><span class="o">.</span><span class="n">_prefetched_objects_cache</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">through_attr</span><span class="p">))</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="k">try</span><span class="p">:</span>
                            <span class="n">new_obj</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">through_attr</span><span class="p">)</span>
                        <span class="k">except</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">ObjectDoesNotExist</span><span class="p">:</span>
                            <span class="k">continue</span>
                    <span class="k">if</span> <span class="n">new_obj</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                        <span class="k">continue</span>
                    <span class="c1"># We special-case `list` rather than something more generic</span>
                    <span class="c1"># like `Iterable` because we don&#39;t want to accidentally match</span>
                    <span class="c1"># user models that define __iter__.</span>
                    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">new_obj</span><span class="p">,</span> <span class="nb">list</span><span class="p">):</span>
                        <span class="n">new_obj_list</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">new_obj</span><span class="p">)</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">new_obj_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">new_obj</span><span class="p">)</span>
                <span class="n">obj_list</span> <span class="o">=</span> <span class="n">new_obj_list</span></div>


<span class="k">def</span> <span class="nf">get_prefetcher</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">through_attr</span><span class="p">,</span> <span class="n">to_attr</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    For the attribute &#39;through_attr&#39; on the given instance, find</span>
<span class="sd">    an object that has a get_prefetch_queryset().</span>
<span class="sd">    Return a 4 tuple containing:</span>
<span class="sd">    (the object with get_prefetch_queryset (or None),</span>
<span class="sd">     the descriptor object representing this relationship (or None),</span>
<span class="sd">     a boolean that is False if the attribute was not found at all,</span>
<span class="sd">     a boolean that is True if the attribute has already been fetched)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">prefetcher</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">is_fetched</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="c1"># For singly related objects, we have to avoid getting the attribute</span>
    <span class="c1"># from the object, as this will trigger the query. So we first try</span>
    <span class="c1"># on the class, in order to get the descriptor object.</span>
    <span class="n">rel_obj_descriptor</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="n">through_attr</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">rel_obj_descriptor</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">attr_found</span> <span class="o">=</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">through_attr</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">attr_found</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">if</span> <span class="n">rel_obj_descriptor</span><span class="p">:</span>
            <span class="c1"># singly related object, descriptor object has the</span>
            <span class="c1"># get_prefetch_queryset() method.</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">rel_obj_descriptor</span><span class="p">,</span> <span class="s1">&#39;get_prefetch_queryset&#39;</span><span class="p">):</span>
                <span class="n">prefetcher</span> <span class="o">=</span> <span class="n">rel_obj_descriptor</span>
                <span class="k">if</span> <span class="n">rel_obj_descriptor</span><span class="o">.</span><span class="n">is_cached</span><span class="p">(</span><span class="n">instance</span><span class="p">):</span>
                    <span class="n">is_fetched</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># descriptor doesn&#39;t support prefetching, so we go ahead and get</span>
                <span class="c1"># the attribute on the instance rather than the class to</span>
                <span class="c1"># support many related managers</span>
                <span class="n">rel_obj</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">through_attr</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">rel_obj</span><span class="p">,</span> <span class="s1">&#39;get_prefetch_queryset&#39;</span><span class="p">):</span>
                    <span class="n">prefetcher</span> <span class="o">=</span> <span class="n">rel_obj</span>
                <span class="k">if</span> <span class="n">through_attr</span> <span class="o">!=</span> <span class="n">to_attr</span><span class="p">:</span>
                    <span class="c1"># Special case cached_property instances because hasattr</span>
                    <span class="c1"># triggers attribute computation and assignment.</span>
                    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">instance</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="n">to_attr</span><span class="p">,</span> <span class="kc">None</span><span class="p">),</span> <span class="n">cached_property</span><span class="p">):</span>
                        <span class="n">is_fetched</span> <span class="o">=</span> <span class="n">to_attr</span> <span class="ow">in</span> <span class="n">instance</span><span class="o">.</span><span class="vm">__dict__</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">is_fetched</span> <span class="o">=</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">instance</span><span class="p">,</span> <span class="n">to_attr</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">is_fetched</span> <span class="o">=</span> <span class="n">through_attr</span> <span class="ow">in</span> <span class="n">instance</span><span class="o">.</span><span class="n">_prefetched_objects_cache</span>
    <span class="k">return</span> <span class="n">prefetcher</span><span class="p">,</span> <span class="n">rel_obj_descriptor</span><span class="p">,</span> <span class="n">attr_found</span><span class="p">,</span> <span class="n">is_fetched</span>


<span class="k">def</span> <span class="nf">prefetch_one_level</span><span class="p">(</span><span class="n">instances</span><span class="p">,</span> <span class="n">prefetcher</span><span class="p">,</span> <span class="n">lookup</span><span class="p">,</span> <span class="n">level</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Helper function for prefetch_related_objects().</span>

<span class="sd">    Run prefetches on all instances using the prefetcher object,</span>
<span class="sd">    assigning results to relevant caches in instance.</span>

<span class="sd">    Return the prefetched objects along with any additional prefetches that</span>
<span class="sd">    must be done due to prefetch_related lookups found from default managers.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c1"># prefetcher must have a method get_prefetch_queryset() which takes a list</span>
    <span class="c1"># of instances, and returns a tuple:</span>

    <span class="c1"># (queryset of instances of self.model that are related to passed in instances,</span>
    <span class="c1">#  callable that gets value to be matched for returned instances,</span>
    <span class="c1">#  callable that gets value to be matched for passed in instances,</span>
    <span class="c1">#  boolean that is True for singly related objects,</span>
    <span class="c1">#  cache or field name to assign to,</span>
    <span class="c1">#  boolean that is True when the previous argument is a cache name vs a field name).</span>

    <span class="c1"># The &#39;values to be matched&#39; must be hashable as they will be used</span>
    <span class="c1"># in a dictionary.</span>

    <span class="n">rel_qs</span><span class="p">,</span> <span class="n">rel_obj_attr</span><span class="p">,</span> <span class="n">instance_attr</span><span class="p">,</span> <span class="n">single</span><span class="p">,</span> <span class="n">cache_name</span><span class="p">,</span> <span class="n">is_descriptor</span> <span class="o">=</span> <span class="p">(</span>
        <span class="n">prefetcher</span><span class="o">.</span><span class="n">get_prefetch_queryset</span><span class="p">(</span><span class="n">instances</span><span class="p">,</span> <span class="n">lookup</span><span class="o">.</span><span class="n">get_current_queryset</span><span class="p">(</span><span class="n">level</span><span class="p">)))</span>
    <span class="c1"># We have to handle the possibility that the QuerySet we just got back</span>
    <span class="c1"># contains some prefetch_related lookups. We don&#39;t want to trigger the</span>
    <span class="c1"># prefetch_related functionality by evaluating the query. Rather, we need</span>
    <span class="c1"># to merge in the prefetch_related lookups.</span>
    <span class="c1"># Copy the lookups in case it is a Prefetch object which could be reused</span>
    <span class="c1"># later (happens in nested prefetch_related).</span>
    <span class="n">additional_lookups</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">additional_lookup</span><span class="p">)</span> <span class="k">for</span> <span class="n">additional_lookup</span>
        <span class="ow">in</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">rel_qs</span><span class="p">,</span> <span class="s1">&#39;_prefetch_related_lookups&#39;</span><span class="p">,</span> <span class="p">())</span>
    <span class="p">]</span>
    <span class="k">if</span> <span class="n">additional_lookups</span><span class="p">:</span>
        <span class="c1"># Don&#39;t need to clone because the manager should have given us a fresh</span>
        <span class="c1"># instance, so we access an internal instead of using public interface</span>
        <span class="c1"># for performance reasons.</span>
        <span class="n">rel_qs</span><span class="o">.</span><span class="n">_prefetch_related_lookups</span> <span class="o">=</span> <span class="p">()</span>

    <span class="n">all_related_objects</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">rel_qs</span><span class="p">)</span>

    <span class="n">rel_obj_cache</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">rel_obj</span> <span class="ow">in</span> <span class="n">all_related_objects</span><span class="p">:</span>
        <span class="n">rel_attr_val</span> <span class="o">=</span> <span class="n">rel_obj_attr</span><span class="p">(</span><span class="n">rel_obj</span><span class="p">)</span>
        <span class="n">rel_obj_cache</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">rel_attr_val</span><span class="p">,</span> <span class="p">[])</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">rel_obj</span><span class="p">)</span>

    <span class="n">to_attr</span><span class="p">,</span> <span class="n">as_attr</span> <span class="o">=</span> <span class="n">lookup</span><span class="o">.</span><span class="n">get_current_to_attr</span><span class="p">(</span><span class="n">level</span><span class="p">)</span>
    <span class="c1"># Make sure `to_attr` does not conflict with a field.</span>
    <span class="k">if</span> <span class="n">as_attr</span> <span class="ow">and</span> <span class="n">instances</span><span class="p">:</span>
        <span class="c1"># We assume that objects retrieved are homogeneous (which is the premise</span>
        <span class="c1"># of prefetch_related), so what applies to first object applies to all.</span>
        <span class="n">model</span> <span class="o">=</span> <span class="n">instances</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="vm">__class__</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">model</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">to_attr</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">exceptions</span><span class="o">.</span><span class="n">FieldDoesNotExist</span><span class="p">:</span>
            <span class="k">pass</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s1">&#39;to_attr=</span><span class="si">{}</span><span class="s1"> conflicts with a field on the </span><span class="si">{}</span><span class="s1"> model.&#39;</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="n">msg</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">to_attr</span><span class="p">,</span> <span class="n">model</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>

    <span class="c1"># Whether or not we&#39;re prefetching the last part of the lookup.</span>
    <span class="n">leaf</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">lookup</span><span class="o">.</span><span class="n">prefetch_through</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">LOOKUP_SEP</span><span class="p">))</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">==</span> <span class="n">level</span>

    <span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">instances</span><span class="p">:</span>
        <span class="n">instance_attr_val</span> <span class="o">=</span> <span class="n">instance_attr</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
        <span class="n">vals</span> <span class="o">=</span> <span class="n">rel_obj_cache</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">instance_attr_val</span><span class="p">,</span> <span class="p">[])</span>

        <span class="k">if</span> <span class="n">single</span><span class="p">:</span>
            <span class="n">val</span> <span class="o">=</span> <span class="n">vals</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="n">vals</span> <span class="k">else</span> <span class="kc">None</span>
            <span class="k">if</span> <span class="n">as_attr</span><span class="p">:</span>
                <span class="c1"># A to_attr has been given for the prefetch.</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">to_attr</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">is_descriptor</span><span class="p">:</span>
                <span class="c1"># cache_name points to a field name in obj.</span>
                <span class="c1"># This field is a descriptor for a related object.</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">cache_name</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># No to_attr has been given for this prefetch operation and the</span>
                <span class="c1"># cache_name does not point to a descriptor. Store the value of</span>
                <span class="c1"># the field in the object&#39;s field cache.</span>
                <span class="n">obj</span><span class="o">.</span><span class="n">_state</span><span class="o">.</span><span class="n">fields_cache</span><span class="p">[</span><span class="n">cache_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">val</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">as_attr</span><span class="p">:</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">to_attr</span><span class="p">,</span> <span class="n">vals</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">manager</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">to_attr</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">leaf</span> <span class="ow">and</span> <span class="n">lookup</span><span class="o">.</span><span class="n">queryset</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                    <span class="n">qs</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">_apply_rel_filters</span><span class="p">(</span><span class="n">lookup</span><span class="o">.</span><span class="n">queryset</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">qs</span> <span class="o">=</span> <span class="n">manager</span><span class="o">.</span><span class="n">get_queryset</span><span class="p">()</span>
                <span class="n">qs</span><span class="o">.</span><span class="n">_result_cache</span> <span class="o">=</span> <span class="n">vals</span>
                <span class="c1"># We don&#39;t want the individual qs doing prefetch_related now,</span>
                <span class="c1"># since we have merged this into the current work.</span>
                <span class="n">qs</span><span class="o">.</span><span class="n">_prefetch_done</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="n">obj</span><span class="o">.</span><span class="n">_prefetched_objects_cache</span><span class="p">[</span><span class="n">cache_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">qs</span>
    <span class="k">return</span> <span class="n">all_related_objects</span><span class="p">,</span> <span class="n">additional_lookups</span>


<span class="k">class</span> <span class="nc">RelatedPopulator</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    RelatedPopulator is used for select_related() object instantiation.</span>

<span class="sd">    The idea is that each select_related() model will be populated by a</span>
<span class="sd">    different RelatedPopulator instance. The RelatedPopulator instances get</span>
<span class="sd">    klass_info and select (computed in SQLCompiler) plus the used db as</span>
<span class="sd">    input for initialization. That data is used to compute which columns</span>
<span class="sd">    to use, how to instantiate the model, and how to populate the links</span>
<span class="sd">    between the objects.</span>

<span class="sd">    The actual creation of the objects is done in populate() method. This</span>
<span class="sd">    method gets row and from_obj as input and populates the select_related()</span>
<span class="sd">    model instance.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">klass_info</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="n">db</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">db</span> <span class="o">=</span> <span class="n">db</span>
        <span class="c1"># Pre-compute needed attributes. The attributes are:</span>
        <span class="c1">#  - model_cls: the possibly deferred model class to instantiate</span>
        <span class="c1">#  - either:</span>
        <span class="c1">#    - cols_start, cols_end: usually the columns in the row are</span>
        <span class="c1">#      in the same order model_cls.__init__ expects them, so we</span>
        <span class="c1">#      can instantiate by model_cls(*row[cols_start:cols_end])</span>
        <span class="c1">#    - reorder_for_init: When select_related descends to a child</span>
        <span class="c1">#      class, then we want to reuse the already selected parent</span>
        <span class="c1">#      data. However, in this case the parent data isn&#39;t necessarily</span>
        <span class="c1">#      in the same order that Model.__init__ expects it to be, so</span>
        <span class="c1">#      we have to reorder the parent data. The reorder_for_init</span>
        <span class="c1">#      attribute contains a function used to reorder the field data</span>
        <span class="c1">#      in the order __init__ expects it.</span>
        <span class="c1">#  - pk_idx: the index of the primary key field in the reordered</span>
        <span class="c1">#    model data. Used to check if a related object exists at all.</span>
        <span class="c1">#  - init_list: the field attnames fetched from the database. For</span>
        <span class="c1">#    deferred models this isn&#39;t the same as all attnames of the</span>
        <span class="c1">#    model&#39;s fields.</span>
        <span class="c1">#  - related_populators: a list of RelatedPopulator instances if</span>
        <span class="c1">#    select_related() descends to related models from this model.</span>
        <span class="c1">#  - local_setter, remote_setter: Methods to set cached values on</span>
        <span class="c1">#    the object being populated and on the remote object. Usually</span>
        <span class="c1">#    these are Field.set_cached_value() methods.</span>
        <span class="n">select_fields</span> <span class="o">=</span> <span class="n">klass_info</span><span class="p">[</span><span class="s1">&#39;select_fields&#39;</span><span class="p">]</span>
        <span class="n">from_parent</span> <span class="o">=</span> <span class="n">klass_info</span><span class="p">[</span><span class="s1">&#39;from_parent&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">from_parent</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cols_start</span> <span class="o">=</span> <span class="n">select_fields</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">cols_end</span> <span class="o">=</span> <span class="n">select_fields</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">init_list</span> <span class="o">=</span> <span class="p">[</span>
                <span class="n">f</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">attname</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">select</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">cols_start</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">cols_end</span><span class="p">]</span>
            <span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reorder_for_init</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">attname_indexes</span> <span class="o">=</span> <span class="p">{</span><span class="n">select</span><span class="p">[</span><span class="n">idx</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">target</span><span class="o">.</span><span class="n">attname</span><span class="p">:</span> <span class="n">idx</span> <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="n">select_fields</span><span class="p">}</span>
            <span class="n">model_init_attnames</span> <span class="o">=</span> <span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">attname</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">klass_info</span><span class="p">[</span><span class="s1">&#39;model&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">concrete_fields</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">init_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">attname</span> <span class="k">for</span> <span class="n">attname</span> <span class="ow">in</span> <span class="n">model_init_attnames</span> <span class="k">if</span> <span class="n">attname</span> <span class="ow">in</span> <span class="n">attname_indexes</span><span class="p">]</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">reorder_for_init</span> <span class="o">=</span> <span class="n">operator</span><span class="o">.</span><span class="n">itemgetter</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="n">attname_indexes</span><span class="p">[</span><span class="n">attname</span><span class="p">]</span> <span class="k">for</span> <span class="n">attname</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">init_list</span><span class="p">])</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">model_cls</span> <span class="o">=</span> <span class="n">klass_info</span><span class="p">[</span><span class="s1">&#39;model&#39;</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pk_idx</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">init_list</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model_cls</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">pk</span><span class="o">.</span><span class="n">attname</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">related_populators</span> <span class="o">=</span> <span class="n">get_related_populators</span><span class="p">(</span><span class="n">klass_info</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">local_setter</span> <span class="o">=</span> <span class="n">klass_info</span><span class="p">[</span><span class="s1">&#39;local_setter&#39;</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">remote_setter</span> <span class="o">=</span> <span class="n">klass_info</span><span class="p">[</span><span class="s1">&#39;remote_setter&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">populate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">row</span><span class="p">,</span> <span class="n">from_obj</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">reorder_for_init</span><span class="p">:</span>
            <span class="n">obj_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reorder_for_init</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">obj_data</span> <span class="o">=</span> <span class="n">row</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">cols_start</span><span class="p">:</span><span class="bp">self</span><span class="o">.</span><span class="n">cols_end</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">obj_data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">pk_idx</span><span class="p">]</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">obj</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model_cls</span><span class="o">.</span><span class="n">from_db</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">db</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">init_list</span><span class="p">,</span> <span class="n">obj_data</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">rel_iter</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">related_populators</span><span class="p">:</span>
                <span class="n">rel_iter</span><span class="o">.</span><span class="n">populate</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">local_setter</span><span class="p">(</span><span class="n">from_obj</span><span class="p">,</span> <span class="n">obj</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">obj</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">remote_setter</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">from_obj</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">get_related_populators</span><span class="p">(</span><span class="n">klass_info</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="n">db</span><span class="p">):</span>
    <span class="n">iterators</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">related_klass_infos</span> <span class="o">=</span> <span class="n">klass_info</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;related_klass_infos&#39;</span><span class="p">,</span> <span class="p">[])</span>
    <span class="k">for</span> <span class="n">rel_klass_info</span> <span class="ow">in</span> <span class="n">related_klass_infos</span><span class="p">:</span>
        <span class="n">rel_cls</span> <span class="o">=</span> <span class="n">RelatedPopulator</span><span class="p">(</span><span class="n">rel_klass_info</span><span class="p">,</span> <span class="n">select</span><span class="p">,</span> <span class="n">db</span><span class="p">)</span>
        <span class="n">iterators</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">rel_cls</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">iterators</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>